Vue中组件化编码的大致流程(初接触)、组件之间的参数传递(最基础的形式)、组件之间的配合完成一个需求

1、在Vue中进行组件化编码

1.1、组件化编码流程:

  • (1)、拆分静态组件:组件要按照功能点拆分,命名不要与html元素冲突。

  • (2)、实现动态组件:考虑好数据的存放位置,数据是一个组件在用,还是一些组件在用:

    ​ 1)、一个组件在用:放在组件自身即可。

    ​ 2)、 一些组件在用:放在他们共同的父组件上(状态提升)。

  • (3)、实现交互:从绑定事件开始。

1.2、props适用于:

  • (1)、父组件 ==> 子组件 通信

  • (2)、子组件 ==> 父组件 通信(要求父先给子一个函数)

1.3 、注意事项

使用v-model时要切记:v-model绑定的值不能是props传过来的值,因为props是不可以修改的

props传过来的若是对象类型的值,修改对象中的属性时Vue不会报错,但不推荐这样做。

2、任务需求(需要实现的效果)


3、项目结构

4、代码实例

4.1 TheFooter.vue

<template>
<div class="todo-footer" > <!-- <input type="checkbox" :checked="isAll" @change="checkAll"/> -->
<input type="checkbox" /> <span> 已完成2/全部3</span>
<button class="btn btn-danger">清除已完成任务</button>
</div>
</template> <script>
export default {
name: "TheFooter", methods: {},
};
</script> <style scoped>
/*footer*/
.todo-footer {
height: 40px;
line-height: 40px;
padding-left: 6px;
margin-top: 5px;
} .todo-footer label {
display: inline-block;
margin-right: 20px;
cursor: pointer;
} .todo-footer label input {
position: relative;
top: -1px;
vertical-align: middle;
margin-right: 5px;
} .todo-footer button {
float: right;
margin-top: 5px;
}
</style>

4.2 TheHeader.vue

<template>
<div class="todo-header">
<input type="text" placeholder="请输入你的任务名称,按回车键确认" />
</div>
</template> <script>
export default {
name: "TheHeader", data() {
return {};
},
methods: {},
};
</script> <style scoped>
/*header*/
.todo-header input {
width: 560px;
height: 28px;
font-size: 14px;
border: 1px solid #ccc;
border-radius: 4px;
padding: 4px 7px;
} .todo-header input:focus {
outline: none;
border-color: rgba(82, 168, 236, 0.8);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
0 0 8px rgba(82, 168, 236, 0.6);
}
</style>

4.3 TheItem.vue

<template>
<div>
<li>
<label>
<input type="checkbox" />
<span>吃饭</span>
</label>
<button>删除</button>
</li> <li>
<label>
<input type="checkbox" />
<span>睡觉</span>
</label>
<button>删除</button>
</li> <li>
<label>
<input type="checkbox" />
<span>打豆豆</span>
</label>
<button>删除</button>
</li>
</div>
</template> <script>
export default {
name: "MyItem",
data() {}, methods: {},
};
</script> <style scoped>
/*item*/
li {
list-style: none;
height: 36px;
line-height: 36px;
padding: 0 5px;
border-bottom: 1px solid #ddd;
} li label {
float: left;
cursor: pointer;
} li label li input {
vertical-align: middle;
margin-right: 6px;
position: relative;
top: -1px;
} li button {
float: right; margin-top: 3px;
} li:before {
content: initial;
} li:last-child {
border-bottom: none;
} li:hover {
background-color: #ddd;
} li:hover button {
display: block;
}
</style>

4.4 TheList.vue

<template>
<ul class="todo-main">
<TheItem />
</ul>
</template> <script>
import TheItem from "./TheItem"; export default {
name: "TheList",
components: { TheItem }, };
</script> <style scoped>
/*main*/
.todo-main {
margin-left: 0px;
border: 1px solid #ddd;
border-radius: 2px;
padding: 0px;
} .todo-empty {
height: 40px;
line-height: 40px;
border: 1px solid #ddd;
border-radius: 2px;
padding-left: 5px;
margin-top: 10px;
}
</style>

4.5 App.vue

<template>
<div id="root">
<div class="todo-container">
<div class="todo-wrap">
<TheHeader />
<TheList />
<TheFooter />
</div>
</div>
</div>
</template> <script>
import TheHeader from "./components/TheHeader";
import TheList from "./components/TheList";
import TheFooter from "./components/TheFooter.vue"; export default {
name: "App",
components: { TheHeader, TheList, TheFooter },
data() {
return {};
},
methods: {},
};
</script> <style>
/*base*/
body {
background: #fff;
}
.btn {
display: inline-block;
padding: 4px 12px;
margin-bottom: 0;
font-size: 14px;
line-height: 20px;
text-align: center;
vertical-align: middle;
cursor: pointer;
box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
0 1px 2px rgba(0, 0, 0, 0.05);
border-radius: 4px;
}
.btn-danger {
color: #fff;
background-color: #da4f49;
border: 1px solid #bd362f;
}
.btn-danger:hover {
color: #fff;
background-color: #bd362f;
}
.btn:focus {
outline: none;
}
.todo-container {
width: 600px;
margin: 0 auto;
}
.todo-container .todo-wrap {
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
}
</style>

4.6 main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router' Vue.config.productionTip = false /* eslint-disable no-new */
new Vue({
el: '#app',
router,
components: { App },
template: '<App/>'
})

5、静态页面效果(功能还未添加)


Vue中组件化编码使用(实战练习一)的更多相关文章

  1. Vue中组件化编码使用、实现组件之间的参数传递(实战练习二)

    上一章节实现的是静态页面的设计.这一章节实现将数据抽取出来.通过组件间参数的传递来实现 上一章节链接地址:https://blog.csdn.net/weixin_43304253/article/d ...

  2. Vue中组件化编码 完成任务的添加、删除、统计、勾选需求(实战练习三完结)

    上一个章节实现数据在组件之间的传递 .这一章主要是完成添加任务到任务栏.删除任务栏.统计任务完成情况.主要还是参数在各个组件之间的传递. 上一章节的链接地址:https://blog.csdn.net ...

  3. 4.VUE前端框架学习记录四:Vue组件化编码2

    VUE前端框架学习记录四:Vue组件化编码2文字信息没办法描述清楚,主要看编码Demo里面,有附带完整的代码下载地址,有需要的同学到脑图里面自取.脑图地址http://naotu.baidu.com/ ...

  4. 3.VUE前端框架学习记录三:Vue组件化编码1

    VUE前端框架学习记录三:Vue组件化编码1文字信息没办法描述清楚,主要看编码Demo里面,有附带完整的代码下载地址,有需要的同学到脑图里面自取.脑图地址http://naotu.baidu.com/ ...

  5. VUE.JS组件化

    VUE.JS组件化 前言 公司目前制作一个H5活动,特别是有一定统一结构的活动,都要码一个重复的轮子.后来接到一个基于模板的活动设计系统的需求,便有了下面的内容.借油开车. 组件化 需求一到,接就是怎 ...

  6. vue的组件化运用(数据在两个组件互传,小问题总结)

    一.vue的组件化应用 首先,知道有哪些相关的属性需要用到,再慢慢去理解,运用. 1.两个vue页面 2. slot占位符(可用可不用) 3.props内置属性 4.watch监听函数 5.impor ...

  7. 【06】Vue 之 组件化开发

    组件其实就是一个拥有样式.动画.js逻辑.HTML结构的综合块.前端组件化确实让大的前端团队更高效的开发前端项目.而作为前端比较流行的框架之一,Vue的组件和也做的非常彻底,而且有自己的特色.尤其是她 ...

  8. Vue中组件

    0828自我总结 Vue中组件 一.组件的构成 组件:由 template + css + js 三部分组成(.vue文件) 1)组件具有复用性 2) 复用组件时,数据要隔离 3) 复用组件时,方法不 ...

  9. vue中组件之间的通信

    一.vue中组件通信的种类 父组件向子组件的通信 子组件向父组件的通信 隔代组件之间的通信 兄弟 组件 之间的通信 二.实现通信的方式  props vue自定义的事件 消息订阅与发布 vuex sl ...

随机推荐

  1. Apache DolphinScheduler新一代分布式工作流任务调度平台实战-中

    @ 目录 架构设计 总体架构 启动流程图 架构设计思想简述 负载均衡 缓存 实战使用 参数 参数优先级 内置参数 基础内置参数 衍生内置参数 本地参数和全局参数 工作流传参 数据源管理 支持数据源 创 ...

  2. Hammersley采样类定义和测试

    原理参照书籍 类声明: #pragma once #ifndef __HAMMERSLEY_HEADER__ #define __HAMMERSLEY_HEADER__ #include " ...

  3. MES对接Simba实现展讯平台 IMEI 写号与耦合测试

    文章开始之前,必须对Simba工具点一个大大的赞,Simba为了适应市面上不同厂家开发的 MES 系统,特地开发了统一的接口,各个 MES 厂家只需要按照接口规范去做开发,然后将中间件加载到 Simb ...

  4. vue中vuex实现持久化的几种方法

    前提:大家都知道vuex真的数据共享是不持久的,例如登录后一刷新,state中存的token就会消失,导致你需要再次进行登录操作 在这给大家列出几种解决方案: 第一种(也是一些项目中常使用的):使用缓 ...

  5. Spring源码 16 IOC refresh方法11

    参考源 https://www.bilibili.com/video/BV1tR4y1F75R?spm_id_from=333.337.search-card.all.click https://ww ...

  6. MySQL-报错:Error when bootstrapping CMake:

    在进行MySQL的源码安装的时候,系统上找不到合适的C编译器,GCC忘了装,莫慌,直接  yum命令装上gcc,还有gcc-C++没装的话后面也会提示错误,一起装上,,, [root@localhos ...

  7. docker compose搭建redis7.0.4高可用一主二从三哨兵集群并整合SpringBoot【图文完整版】

    一.前言 redis在我们企业级开发中是很常见的,但是单个redis不能保证我们的稳定使用,所以我们要建立一个集群. redis有两种高可用的方案: High availability with Re ...

  8. .NET 6应用程序适配国产银河麒麟V10系统随记

    最近想在麒麟系统上运行.NET 6程序,经过一番折腾最终完成了,简单记录一下. 目标系统: CPU: aarch64架构(ARM64) 操作系统:银河麒麟V10高级服务器系统 银河麒麟V10系统(以下 ...

  9. 算法模板:spfa

    #include<iostream> #include<algorithm> #include<cstring> #include<string> #i ...

  10. [ARC119E] Pancakes (二维偏序,分类讨论)

    题面 一个长为 N N N 的序列 S S S ,最多翻转序列中一个区间,最小化 ∑ i = 2 N ∣ S i − S i − 1 ∣ \sum_{i=2}^{N}|S_i-S_{i-1}| i=2 ...