哪种语言做网站最快,免费微信网站模板下载工具,公司网站建站流程,大连图文设计有限公司1.Vue组件化编程(只有1个数字是一级标题)
1.1 模块与组件、模块化与组件化(两个数字组成是二级标题) 1.1.1模块(三个数字是三级标题 依次类推)
理解#xff1a;向外提供特定功能的 js 程序#xff0c;一般就是一个 js 文件为什么#xff1a;js 文件很多很复杂作用#xf…1.Vue组件化编程(只有1个数字是一级标题)
1.1 模块与组件、模块化与组件化(两个数字组成是二级标题) 1.1.1模块(三个数字是三级标题 依次类推)
理解向外提供特定功能的 js 程序一般就是一个 js 文件为什么js 文件很多很复杂作用复用 js简化 js 的编写提高 js 运行效率
1.1.2. 组件
定义用来实现局部功能的代码和资源的集合html/css/js/image…为什么一个界面的功能很复杂作用复用编码简化项目编码提高运行效率
1.1.3. 模块化
当应用中的 js 都以模块来编写的那这个应用就是一个模块化的应用
1.1.4. 组件化
当应用中的功能都是多组件的方式来编写的那这个应用就是一个组件化的应用
2.2. 非单文件组件
2.2.1. 基本使用
!DOCTYPE html
htmlheadmeta charsetUTF-8 /title基本使用/titlescript typetext/javascript src../js/vue.js/script/headbodydiv idrooth1{{msg}}/h1hr!-- 第三步编写组件标签 --school/schoolhr!-- 第三步编写组件标签 --student/student/div/bodyscript typetext/javascriptVue.config.productionTip false//第一步创建school组件const school Vue.extend({//组件定义时一定不要写el配置项因为最终所有的组件都要被一个vm管理由vm决定服务于哪个容器。template:div classdemoh2学校名称{{schoolName}}/h2h2学校地址{{address}}/h2 /div,data(){return {schoolName:尚硅谷,address:北京昌平}}})//第一步创建student组件const student Vue.extend({template:divh2学生姓名{{studentName}}/h2h2学生年龄{{age}}/h2/div,data(){return {studentName:JOJO,age:20}}})//创建vmnew Vue({el:#root,data:{msg:你好JOJO},//第二步注册组件局部注册components:{school,student}})/script
/html总结
Vue中使用组件的三大步骤
定义组件(创建组件)注册组件使用组件(写组件标签)
如何定义一个组件 使用Vue.extend(options)创建其中options和new Vue(options)时传入的options几乎一样但也有点区别 1.el不要写为什么 最终所有的组件都要经过一个vm的管理由vm中的el决定服务哪个容器 2.data必须写成函数为什么 避免组件被复用时数据存在引用关系 如何注册组件 局部注册new Vue的时候传入components选项全局注册Vue.component(组件名,组件)
编写组件标签school/school 2.2.2. 组件注意事项
!DOCTYPE html
htmlheadmeta charsetUTF-8 /title组件注意事项/titlescript typetext/javascript src../js/vue.js/script/headbodydiv idrooth1{{msg}}/h1school/school/div/bodyscript typetext/javascriptVue.config.productionTip falseconst school Vue.extend({name:atguigu,template:divh2学校名称{{name}}/h2 h2学校地址{{address}}/h2 /div,data(){return {name:尚硅谷,address:北京}}})new Vue({el:#root,data:{msg:欢迎学习Vue!},components:{school}})/script
/html总结:
关于组件名
一个单词组成
第一种写法首字母小写school第二种写法首字母大写School
多个单词组成
第一种写法kebab-case命名my-school第二种写法CamelCase命名MySchool 需要Vue脚手架支持
备注
组件名尽可能回避HTML中已有的元素名称例如h2、H2都不行可以使用name配置项指定组件在开发者工具中呈现的名字
关于组件标签
第一种写法school/school第二种写法school/备注不使用脚手架时school/会导致后续组件不能渲染
一个简写方式const school Vue.extend(options)可简写为const school options
2.2.3. 组件的嵌套
!DOCTYPE html
htmlheadmeta charsetUTF-8 /title组件的嵌套/titlescript typetext/javascript src../js/vue.js/script/headbodydiv idroot/div/bodyscript typetext/javascriptVue.config.productionTip false//定义student组件const student Vue.extend({template:divh2学生名称{{name}}/h2 h2学生年龄{{age}}/h2 /div,data(){return {name:JOJO,age:20}}})//定义school组件const school Vue.extend({template:divh2学校名称{{name}}/h2 h2学校地址{{address}}/h2 student/student/div,components:{student},data(){return {name:尚硅谷,address:北京}}})//定义hello组件const hello Vue.extend({template:h1{{msg}}/h1,data(){return {msg:欢迎学习尚硅谷Vue教程}}})//定义app组件const app Vue.extend({template:divhello/helloschool/school/div,components:{school,hello}})//创建vmnew Vue({template:app/app,el:#root,components:{app}})/script
/html2.2.4. VueComponent
关于VueComponent
school组件本质是一个名为VueComponent的构造函数且不是程序员定义的是Vue.extend生成的我们只需要写school/或school/schoolVue解析时会帮我们创建school组件的实例对象即Vue帮我们执行的new VueComponent(options)特别注意每次调用Vue.extend返回的都是一个全新的VueComponent关于this指向
组件配置中data函数、methods中的函数、watch中的函数、computed中的函数 它们的this均是VueComponent实例对象new Vue(options)配置中data函数、methods中的函数、watch中的函数、computed中的函数 它们的this均是Vue实例对象
5.VueComponent的实例对象以后简称vc也可称之为组件实例对象 Vue的实例对象以后简称vm 只有在本笔记中VueComponent的实例对象才简称为vc 2.2.5. 一个重要的内置关系
!DOCTYPE html
htmlheadmeta charsetUTF-8 /title一个重要的内置关系/titlescript typetext/javascript src../js/vue.js/script/headbodydiv idrootschool/school/div/bodyscript typetext/javascriptVue.config.productionTip falseVue.prototype.x 99const school Vue.extend({name:school,template:divh2学校名称{{name}}/h2 h2学校地址{{address}}/h2 button clickshowX点我输出x/button/div,data(){return {name:尚硅谷,address:北京}},methods: {showX(){console.log(this.x)}},})const vm new Vue({el:#root,data:{msg:你好},components:{school}})/script
/html一个重要的内置关系VueComponent.prototype.__proto__ Vue.prototype为什么要有这个关系让组件实例对象vc可以访问到 Vue 原型上的属性、方法
2.3. 单文件组件
School.vue:
templatediv idDemoh2学校名称{{name}}/h2h2学校地址{{address}}/h2button clickshowName点我提示学校名/button/div
/templatescriptexport default {name:School,data() {return {name:尚硅谷,address:北京}},methods: {showName(){alert(this.name)}},}
/scriptstyle#Demo{background: orange;}
/styleStudent.vue:
templatedivh2学生姓名{{name}}/h2h2学生年龄{{age}}/h2/div
/templatescriptexport default {name:Student,data() {return {name:JOJO,age:20}},}
/scriptApp.vue:
templatedivSchool/SchoolStudent/Student/div
/templatescriptimport School from ./School.vueimport Student from ./Student.vueexport default {name:App,components:{School,Student}}
/scriptmain.js:
import App from ./App.vuenew Vue({template:App/App,el:#root,components:{App}
})index.html
!DOCTYPE html
html langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0title单文件组件练习/title
/head
bodydiv idroot/divscript src../../js/vue.js/scriptscript src./main.js/script
/body
/html3. 使用Vue CLI脚手架
3.1. 初始化脚手架
3.1.1. 说明
Vue 脚手架是 Vue 官方提供的标准化开发工具开发平台最新的版本是 4.x文档Vue CLI
3.1.2. 具体步骤
如果下载缓慢请配置 npm 淘宝镜像npm config set registry http://registry.npm.taobao.org全局安装vue/clinpm install -g vue/cli切换到你要创建项目的目录然后使用命令创建项目vue create xxxx选择使用vue的版本启动项目npm run serve暂停项目CtrlC
3.1.3. 分析脚手架结构
脚手架文件结构
.文件目录
├── node_modules
├── public
│ ├── favicon.ico: 页签图标
│ └── index.html: 主页面
├── src
│ ├── assets: 存放静态资源
│ │ └── logo.png
│ │── component: 存放组件
│ │ └── HelloWorld.vue
│ │── App.vue: 汇总所有组件
│ └── main.js: 入口文件
├── .gitignore: git版本管制忽略的配置
├── babel.config.js: babel的配置文件
├── package.json: 应用包配置文件
├── README.md: 应用描述文件
└── package-lock.json: 包版本控制文件src/components/School.vue:
templatediv idDemoh2学校名称{{name}}/h2h2学校地址{{address}}/h2button clickshowName点我提示学校名/button/div
/templatescriptexport default {name:School,data() {return {name:尚硅谷,address:北京}},methods: {showName() {alert(this.name)}},}
/scriptstyle#Demo{background: orange;}
/stylesrc/components/Student.vue: templatedivh2学生姓名{{name}}/h2h2学生年龄{{age}}/h2/div
/templatescriptexport default {name:Student,data() {return {name:JOJO,age:20}},}
/scriptsrc/App.vue:
templatedivSchool/SchoolStudent/Student/div
/templatescriptimport School from ./components/School.vueimport Student from ./components/Student.vueexport default {name:App,components:{School,Student}}
/scriptsrc/main.js:
import Vue from vue
import App from ./App.vueVue.config.productionTip falsenew Vue({el:#app,render: h h(App),
})public/index.html: !DOCTYPE html
html langheadmeta charsetUTF-8!-- 针对IE浏览器的特殊配置含义是让IE浏览器以最高渲染级别渲染页面 --meta http-equivX-UA-Compatible contentIEedge!-- 开启移动端的理想端口 --meta nameviewport contentwidthdevice-width, initial-scale1.0!-- 配置页签图标 --link relicon href% BASE_URL %favicon.ico!-- 配置网页标题 --title% htmlWebpackPlugin.options.title %/title/headbody!-- 容器 --div idapp/div/body
/html3.1.4. render函数 import Vue from vue
import App from ./App.vueVue.config.productionTip falsenew Vue({el:#app,// 简写形式render: h h(App),// 完整形式// render(createElement){// return createElement(App)// }
})总结 关于不同版本的函数 1.vue.js 与 vue.runtime.xxx.js的区别 1. vue.js 是完整版的 Vue包含核心功能模板解析器 2. vue.runtime.xxx.js 是运行版的 Vue只包含核心功能没有模板解析器 2.因为 vue.runtime.xxx.js 没有模板解析器所以不能使用 template 配置项需要使用 render函数接收到的createElement 函数去指定具体内容 3.1.5. 修改默认配置
vue.config.js 是一个可选的配置文件如果项目的和 package.json 同级的根目录中存在这个文件那么它会被 vue/cli-service 自动加载使用 vue.config.js 可以对脚手架进行个性化定制详见配置参考 | Vue CLI
module.exports {pages: {index: {// 入口entry: src/index/main.js}},// 关闭语法检查lineOnSave:false
} 3.2. ref属性
templatedivh1 reftitle{{msg}}/h1School refsch/button clickshow refbtn点我输出ref/button/div
/templatescriptimport School from ./components/School.vueexport default {name:App,components: { School },data() {return {msg:欢迎学习Vue}},methods:{show(){console.log(this.$refs.title)console.log(this.$refs.sch)console.log(this.$refs.btn)}}}
/script
———————————————— 总结 ref属性
被用来给元素或子组件注册引用信息id的替代者应用在html标签上获取的是真实DOM元素应用在组件标签上获取的是组件实例对象vc使用方式 1.打标识h1 refxxx/h1 或 School refxxx/School 2.获取this.$refs.xxx 3.3. props配置项 src/App.vue:
templatedivStudent nameJOJO sex男酮 :age20 //div
/templatescriptimport Student from ./components/Student.vueexport default {name:App,components: { Student },}
————————————————版权声明本文为博主原创文章遵循 CC 4.0 BY-SA 版权协议转载请附上原文出处链接和本声明。原文链接https://blog.csdn.net/qq_55593227/article/details/119717498 src/components/Student.vue:
templatedivh1{{msg}}/h1h2学生姓名{{name}}/h2h2学生性别{{sex}}/h2h2学生年龄{{age}}/h2 /div
/templatescriptexport default {name:Student,data() {return {msg:我是一名来自枝江大学的男酮嘿嘿我的金轮~~,}},// 简单声明接收// props:[name,age,sex]// 接收的同时对数据进行类型限制/* props:{name:String,age:Number,sex:String} */// 接收的同时对数据进行类型限制 指定默认值 限制必要性props:{name:{type:String,required:true,},age:{type:Number,default:99},sex:{type:String,required:true}}}
/script 总结 props配置项 功能让组件接收外部传过来的数据 传递数据Demo namexxx/ 接收数据 1.第一种方式只接收props:[name] 2.第二种方式限制数据类型props:{name:String} 3.第三种方式限制类型、限制必要性、指定默认值
props:{name:{type:String, //类型required:true, //必要性default:JOJO //默认值}
} 3.4. mixin混入 局部混入 src/mixin.js:
export const mixin {methods: {showName() {alert(this.name)}},mounted() {console.log(你好呀~)}
}src/components/School.vue
templatedivh2 clickshowName学校姓名{{name}}/h2h2学校地址{{address}}/h2 /div
/templatescript//引入混入import {mixin} from ../mixinexport default {name:School,data() {return {name:尚硅谷,address:北京}},mixins:[mixin]}
/script src/components/Student.vue:
templatedivh2 clickshowName学生姓名{{name}}/h2h2学生性别{{sex}}/h2 /div
/templatescript//引入混入import {mixin} from ../mixinexport default {name:Student,data() {return {name:JOJO,sex:男}},mixins:[mixin]}
/scriptsrc/App.vue:
templatedivSchool/hr/Student//div
/templatescriptimport Student from ./components/Student.vueimport School from ./components/School.vueexport default {name:App,components: { Student,School },}
/script全局混入 src/main.js:
import Vue from vue
import App from ./App.vue
import {mixin} from ./mixinVue.config.productionTip false
Vue.mixin(mixin)new Vue({el:#app,render: h h(App)
})总结 mixin混入 功能可以把多个组件共用的配置提取成一个混入对象 使用方式 1.定义混入
const mixin {data(){....},methods:{....}....
} 2.使用混入 1.全局混入Vue.mixin(xxx) 2.局部混入mixins:[xxx] 3.备注 1.组件和混入对象含有同名选项时这些选项将以恰当的方式进行“合并”在发生冲突时以组件优先。
var mixin {data: function () {return {message: hello,foo: abc}}
}new Vue({mixins: [mixin],data () {return {message: goodbye,bar: def}},created () {console.log(this.$data)// { message: goodbye, foo: abc, bar: def }}
})2.同名生命周期钩子将合并为一个数组因此都将被调用。另外混入对象的钩子将在组件自身钩子之前调用。
var mixin {created () {console.log(混入对象的钩子被调用)}
}new Vue({mixins: [mixin],created () {console.log(组件钩子被调用)}
})// 混入对象的钩子被调用
// 组件钩子被调用3.5. plugin插件 src/plugin.js:
export default {install(Vue,x,y,z){console.log(x,y,z)//全局过滤器Vue.filter(mySlice,function(value){return value.slice(0,4)})//定义混入Vue.mixin({data() {return {x:100,y:200}},})//给Vue原型上添加一个方法vm和vc就都能用了Vue.prototype.hello (){alert(你好啊)}}
}src/main.js:
import Vue from vue
import App from ./App.vue
import plugin from ./pluginVue.config.productionTip false
Vue.use(plugin,1,2,3)new Vue({el:#app,render: h h(App)
})src/components/School.vue:
templatedivh2学校姓名{{name | mySlice}}/h2h2学校地址{{address}}/h2 /div
/templatescriptexport default {name:School,data() {return {name:尚硅谷atguigu,address:北京}}}
/scriptsrc/components/Student.vue:
templatedivh2学生姓名{{name}}/h2h2学生性别{{sex}}/h2 button clicktest点我测试hello方法/button /div
/templatescriptexport default {name:Student,data() {return {name:JOJO,sex:男}},methods:{test() {this.hello()}}}
/script总结
插件 功能用于增强Vue 本质包含install方法的一个对象install的第一个参数是Vue第二个以后的参数是插件使用者传递的数据 定义插件
plugin.install function (Vue, options) {// 1. 添加全局过滤器Vue.filter(....)// 2. 添加全局指令Vue.directive(....)// 3. 配置全局混入Vue.mixin(....)// 4. 添加实例方法Vue.prototype.$myMethod function () {...}Vue.prototype.$myProperty xxxx}4.使用插件Vue.use(plugin) 3.6. scoped样式 src/components/School.vue:
templatediv classdemoh2学校姓名{{name}}/h2h2学校地址{{address}}/h2 /div
/templatescriptexport default {name:School,data() {return {name:尚硅谷,address:北京}}}
/scriptstyle scoped.demo{background-color: blueviolet;}
/stylesrc/components/Student.vue
templatediv classdemoh2学生姓名{{name}}/h2h2学生性别{{sex}}/h2 /div
/templatescriptexport default {name:Student,data() {return {name:JOJO,sex:男}}}
/scriptstyle scoped.demo{background-color: chartreuse;}
/stylesrc/App.vue:
templatedivSchool/Student//div
/templatescriptimport Student from ./components/Student.vueimport School from ./components/School.vueexport default {name:App,components: { Student,School },}
/script总结 scoped样式
作用让样式在局部生效防止冲突写法style scoped 3.7. Todo-List案例 src/components/MyHeader.vue:
templatediv classtodo-headerinput typetext placeholder请输入你的任务名称按回车键确认 keydown.enteradd v-modeltitle//div
/templatescriptimport {nanoid} from nanoidexport default {name:MyHeader,data() {return {title:}},methods:{add(){if(!this.title.trim()) returnconst todoObj {id:nanoid(),title:this.title,done:false}this.addTodo(todoObj)this.title }},props:[addTodo]}
/scriptstyle scoped.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);}
/stylesrc/components/MyItem.vue:
templatelilabelinput typecheckbox :checkedtodo.done clickhandleCheck(todo.id)/span{{todo.title}}/span/labelbutton classbtn btn-danger clickhandleDelete(todo.id,todo.title)删除/button/li
/templatescriptexport default {name:MyItem,props:[todo,checkTodo,deleteTodo],methods:{handleCheck(id){this.checkTodo(id)},handleDelete(id,title){if(confirm(确定删除任务title吗)){this.deleteTodo(id)}}}}
/scriptstyle scopedli {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;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover {background-color: #eee;}li:hover button{display: block;}
/stylesrc/components/MyList.vue:
templateul classtodo-mainMyItem v-fortodo in todos :keytodo.id :todotodo :checkTodocheckTodo:deleteTododeleteTodo//ul
/templatescriptimport MyItem from ./MyItem.vueexport default {name:MyList,components:{MyItem},props:[todos,checkTodo,deleteTodo]}
/scriptstyle scoped.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;}
/stylesrc/components/MyFooter.vue:
templatediv classtodo-footer v-showtotallabelinput typecheckbox v-modelisAll//labelspanspan已完成{{doneTotal}}/span / 全部{{total}}/spanbutton classbtn btn-danger clickclearAll清除已完成任务/button/div
/templatescriptexport default {name:MyFooter,props:[todos,checkAllTodo,clearAllTodo],computed:{doneTotal(){return this.todos.reduce((pre,todo) pre (todo.done ? 1 : 0) ,0)},total(){return this.todos.length},isAll:{get(){return this.total this.doneTotal this.total 0},set(value){this.checkAllTodo(value)}}},methods:{clearAll(){this.clearAllTodo()}}}
/scriptstyle scoped.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;}
/stylesrc/App.vue:
templatediv idrootdiv classtodo-containerdiv classtodo-wrapMyHeader :addTodoaddTodo/MyList :todostodos :checkTodocheckTodo :deleteTododeleteTodo/MyFooter :todostodos :checkAllTodocheckAllTodo :clearAllTodoclearAllTodo//div/div/div
/templatescriptimport MyHeader from ./components/MyHeader.vueimport MyList from ./components/MyList.vueimport MyFooter from ./components/MyFooter.vueexport default {name:App,components: { MyHeader,MyList,MyFooter },data() {return {todos:[{id:001,title:抽烟,done:false},{id:002,title:喝酒,done:false},{id:003,title:烫头,done:false},]}},methods:{//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckTodo(id){this.todos.forEach((todo){if(todo.id id) todo.done !todo.done})},//删除一个tododeleteTodo(id){this.todos this.todos.filter(todo todo.id ! id)},//全选or取消勾选checkAllTodo(done){this.todos.forEach(todo todo.done done)},//删除已完成的todoclearAllTodo(){this.todos this.todos.filter(todo !todo.done)}}}
/scriptstylebody {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效果 总结
组件化编码流程
拆分静态组件组件要按照功能点拆分命名不要与html元素冲突实现动态组件考虑好数据的存放位置数据是一个组件在用还是一些组件在用 1.一个组件在用放在组件自身即可 2. 一些组件在用放在他们共同的父组件上状态提升 3.实现交互从绑定事件开始 props适用于
父组件 子组件 通信子组件 父组件 通信要求父组件先给子组件一个函数 使用v-model时要切记v-model绑定的值不能是props传过来的值因为props是不可以修改的 props传过来的若是对象类型的值修改对象中的属性时Vue不会报错但不推荐这样做 3.8. WebStorage
!DOCTYPE html
html langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titlelocalStorage/title
/head
bodyh2localStorage/h2button onclicksaveDate()点我保存数据/buttonbr/button onclickreadDate()点我读数据/buttonbr/button onclickdeleteDate()点我删除数据/buttonbr/button onclickdeleteAllDate()点我清空数据/buttonbr/scriptlet person {name:JOJO,age:20}function saveDate(){localStorage.setItem(msg,localStorage)localStorage.setItem(person,JSON.stringify(person))}function readDate(){console.log(localStorage.getItem(msg))const person localStorage.getItem(person)console.log(JSON.parse(person))}function deleteDate(){localStorage.removeItem(msg)localStorage.removeItem(person)}function deleteAllDate(){localStorage.clear()}/script
/body
/html!DOCTYPE html
html langen
headmeta charsetUTF-8meta http-equivX-UA-Compatible contentIEedgemeta nameviewport contentwidthdevice-width, initial-scale1.0titlesessionStorage/title
/head
bodyh2sessionStorage/h2button onclicksaveDate()点我保存数据/buttonbr/button onclickreadDate()点我读数据/buttonbr/button onclickdeleteDate()点我删除数据/buttonbr/button onclickdeleteAllDate()点我清空数据/buttonbr/scriptlet person {name:JOJO,age:20}function saveDate(){sessionStorage.setItem(msg,sessionStorage)sessionStorage.setItem(person,JSON.stringify(person))}function readDate(){console.log(sessionStorage.getItem(msg))const person sessionStorage.getItem(person)console.log(JSON.parse(person))}function deleteDate(){sessionStorage.removeItem(msg)sessionStorage.removeItem(person)}function deleteAllDate(){sessionStorage.clear()}/script
/body
/html总结 存储内容大小一般支持5MB左右不同浏览器可能还不一样 浏览器端通过Window.sessionStorage和Window.localStorage属性来实现本地存储机制 相关API 1.xxxStorage.setItem(key, value)该方法接受一个键和值作为参数会把键值对添加到存储中如果键名存在则更新其对应的值 2. xxxStorage.getItem(key)该方法接受一个键名作为参数返回键名对应的值 3. xxxStorage.removeItem(key)该方法接受一个键名作为参数并把该键名从存储中删除 4.xxxStorage.clear()该方法会清空存储中的所有数据
4.备注 1.SessionStorage存储的内容会随着浏览器窗口关闭而消失 2.LocalStorage存储的内容需要手动清除才会消失 3.xxxStorage.getItem(xxx)如果 xxx 对应的 value 获取不到那么getItem()的返回值是null 4.JSON.parse(null)的结果依然是null 使用本地存储优化Todo-List src/App.vue:
templatediv idrootdiv classtodo-containerdiv classtodo-wrapMyHeader :addTodoaddTodo/MyList :todostodos :checkTodocheckTodo :deleteTododeleteTodo/MyFooter :todostodos :checkAllTodocheckAllTodo :clearAllTodoclearAllTodo//div/div/div
/templatescriptimport MyHeader from ./components/MyHeader.vueimport MyList from ./components/MyList.vueimport MyFooter from ./components/MyFooter.vueexport default {name:App,components: { MyHeader,MyList,MyFooter },data() {return {//若localStorage中存有todos则从localStorage中取出否则初始为空数组todos:JSON.parse(localStorage.getItem(todos)) || []}},methods:{//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckTodo(id){this.todos.forEach((todo){if(todo.id id) todo.done !todo.done})},//删除一个tododeleteTodo(id){this.todos this.todos.filter(todo todo.id ! id)},//全选or取消勾选checkAllTodo(done){this.todos.forEach(todo todo.done done)},//删除已完成的todoclearAllTodo(){this.todos this.todos.filter(todo !todo.done)}},watch:{todos:{//由于todos是对象数组所以必须开启深度监视才能发现数组中对象的变化deep:true,handler(value){localStorage.setItem(todos,JSON.stringify(value))}}}}
/scriptstylebody {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;}
/style3.9. 自定义事件 3.9.1. 绑定 src/App.vue:
templatediv classapp!-- 通过父组件给子组件传递函数类型的props实现子给父传递数据 --School :getSchoolNamegetSchoolName/!-- 通过父组件给子组件绑定一个自定义事件实现子给父传递数据第一种写法使用或v-on --!-- Student jojogetStudentName/ --!-- 通过父组件给子组件绑定一个自定义事件实现子给父传递数据第二种写法使用ref --Student refstudent//div
/templatescriptimport Student from ./components/Student.vueimport School from ./components/School.vueexport default {name:App,components: { Student,School },methods:{getSchoolName(name){console.log(已收到学校的名称name)},getStudentName(name){console.log(已收到学生的姓名name) }},mounted(){this.$refs.student.$on(jojo,this.getStudentName)}}
/scriptstyle scoped.app{background-color: gray;padding: 5px;}
/stylesrc/components/Student.vue:
templatediv classstudenth2学生姓名{{name}}/h2h2学生性别{{sex}}/h2button clicksendStudentName点我传递学生姓名/button /div
/templatescriptexport default {name:Student,data() {return {name:JOJO,sex:男}},methods:{sendStudentName(){this.$emit(jojo,this.name)}}}
/scriptstyle scoped.student{background-color: chartreuse;padding: 5px;margin-top: 30px;}
/style
————————————————3.9.2. 解绑 src/App.vue:
templatediv classappStudent jojogetStudentName//div
/templatescriptimport Student from ./components/Student.vueexport default {name:App,components: { Student },methods:{getStudentName(name){console.log(已收到学生的姓名name) }}}
/scriptstyle scoped.app{background-color: gray;padding: 5px;}
/stylesrc/components/Student.vue:
templatediv classstudenth2学生姓名{{name}}/h2h2学生性别{{sex}}/h2button clicksendStudentName点我传递学生姓名/button button clickunbind解绑自定义事件/button /div
/templatescriptexport default {name:Student,data() {return {name:JOJO,sex:男}},methods:{sendStudentName(){this.$emit(jojo,this.name)},unbind(){// 解绑一个自定义事件// this.$off(jojo)// 解绑多个自定义事件// this.$off([jojo])// 解绑所有自定义事件this.$off()}}}
/scriptstyle scoped.student{background-color: chartreuse;padding: 5px;margin-top: 30px;}
/style总结 组件的自定义事件 一种组件间通信的方式适用于子组件 父组件 使用场景A是父组件B是子组件B想给A传数据那么就要在A中给B绑定自定义事件事件的回调在A中 绑定自定义事件 1.第一种方式在父组件中Demo atguigutest/ 或 Demo von:atguigutest/ 2.第二种方式在父组件中
Demo refdemo/
...
mounted(){this.$refs.demo.$on(atguigu,data)
}3.若想让自定义事件只能触发一次可以使用once修饰符或$once方法 4.触发自定义事件this.$emit(atguigu,数据) 5.解绑自定义事件this.$off(atguigu) 6.组件上也可以绑定原生DOM事件需要使用native修饰符 7.注意通过this.$refs.xxx.$on(atguigu,回调)绑定自定义事件时回调要么配置在methods中要么用箭头函数否则this指向会出问题
使用自定义事件优化Todo-List src/App.vue:
templatediv idrootdiv classtodo-containerdiv classtodo-wrapMyHeader addTodoaddTodo/MyList :todostodos :checkTodocheckTodo :deleteTododeleteTodo/MyFooter :todostodos checkAllTodocheckAllTodo clearAllTodoclearAllTodo//div/div/div
/templatescriptimport MyHeader from ./components/MyHeader.vueimport MyList from ./components/MyList.vueimport MyFooter from ./components/MyFooter.vueexport default {name:App,components: { MyHeader,MyList,MyFooter },data() {return {todos:JSON.parse(localStorage.getItem(todos)) || []}},methods:{//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckTodo(id){this.todos.forEach((todo){if(todo.id id) todo.done !todo.done})},//删除一个tododeleteTodo(id){this.todos this.todos.filter(todo todo.id ! id)},//全选or取消勾选checkAllTodo(done){this.todos.forEach(todo todo.done done)},//删除已完成的todoclearAllTodo(){this.todos this.todos.filter(todo !todo.done)}},watch:{todos:{deep:true,handler(value){localStorage.setItem(todos,JSON.stringify(value))}}}}
/scriptstylebody {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;}
/stylesrc/components/MyHeader.vue:
templatediv classtodo-headerinput typetext placeholder请输入你的任务名称按回车键确认 keydown.enteradd v-modeltitle//div
/templatescriptimport {nanoid} from nanoidexport default {name:MyHeader,data() {return {title:}},methods:{add(){if(!this.title.trim()) returnconst todoObj {id:nanoid(),title:this.title,done:false}this.$emit(addTodo,todoObj)this.title }}}
/scriptstyle 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);}
/stylesrc/components/MyFooter:
templatediv classtodo-footer v-showtotallabelinput typecheckbox v-modelisAll//labelspanspan已完成{{doneTotal}}/span / 全部{{total}}/spanbutton classbtn btn-danger clickclearAll清除已完成任务/button/div
/templatescriptexport default {name:MyFooter,props:[todos],computed:{doneTotal(){return this.todos.reduce((pre,todo) pre (todo.done ? 1 : 0) ,0)},total(){return this.todos.length},isAll:{get(){return this.total this.doneTotal this.total 0},set(value){this.$emit(checkAllTodo,value)}}},methods:{clearAll(){this.$emit(clearAllTodo)}}}
/scriptstyle scoped.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;}
/style3.10. 全局事件总线 src/main.js:
import Vue from vue
import App from ./App.vueVue.config.productionTip falsenew Vue({el:#app,render: h h(App),beforeCreate() {Vue.prototype.$bus this //安装全局事件总线}
})src/App.vue:
templatediv classappSchool/Student//div
/templatescriptimport Student from ./components/Studentimport School from ./components/Schoolexport default {name:App,components:{School,Student}}
/scriptstyle scoped.app{background-color: gray;padding: 5px;}
/stylesrc/components/School.vue:
templatediv classschoolh2学校名称{{name}}/h2h2学校地址{{address}}/h2/div
/templatescriptexport default {name:School,data() {return {name:尚硅谷,address:北京,}},methods:{demo(data) {console.log(我是School组件收到了数据,data)}},mounted() {this.$bus.$on(demo,this.demo)},beforeDestroy() {this.$bus.$off(demo)},}
/scriptstyle scoped.school{background-color: skyblue;padding: 5px;}
/stylesrc/components/Student.vue:
templatediv classstudenth2学生姓名{{name}}/h2h2学生性别{{sex}}/h2button clicksendStudentName把学生名给School组件/button/div
/templatescriptexport default {name:Student,data() {return {name:张三,sex:男}},methods: {sendStudentName(){this.$bus.$emit(demo,this.name)}}}
/scriptstyle scoped.student{background-color: pink;padding: 5px;margin-top: 30px;}
/style总结 全局事件总线GlobalEventBus 一种组件间通信的方式适用于任意组件间通信 安装全局事件总线
new Vue({...beforeCreate() {Vue.prototype.$bus this //安装全局事件总线$bus就是当前应用的vm},...
}) 3.使用事件总线 1.接收数据A组件想接收数据则在A组件中给$bus绑定自定义事件事件的回调留在A组件 自身
export default {methods(){demo(data){...}}...mounted() {this.$bus.$on(xxx,this.demo)}
} 2.提供数据this.$bus.$emit(xxx,data) 4.最好在beforeDestroy钩子中用$off去解绑当前组件所用到的事件 使用自定义事件优化Todo-List src/mian.js: import Vue from vue
import App from ./App.vueVue.config.productionTip falsenew Vue({el:#app,render: h h(App),beforeCreate() {Vue.prototype.$bus this}
})src/components/App.vue
templatediv idrootdiv classtodo-containerdiv classtodo-wrapMyHeader addTodoaddTodo/MyList :todostodos/MyFooter :todostodos checkAllTodocheckAllTodo clearAllTodoclearAllTodo//div/div/div
/templatescriptimport MyHeader from ./components/MyHeader.vueimport MyList from ./components/MyList.vueimport MyFooter from ./components/MyFooter.vueexport default {name:App,components: { MyHeader,MyList,MyFooter },data() {return {todos:JSON.parse(localStorage.getItem(todos)) || []}},methods:{//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckTodo(id){this.todos.forEach((todo){if(todo.id id) todo.done !todo.done})},//删除一个tododeleteTodo(id){this.todos this.todos.filter(todo todo.id ! id)},//全选or取消勾选checkAllTodo(done){this.todos.forEach(todo todo.done done)},//删除已完成的todoclearAllTodo(){this.todos this.todos.filter(todo !todo.done)}},watch:{todos:{deep:true,handler(value){localStorage.setItem(todos,JSON.stringify(value))}}},mounted(){this.$bus.$on(checkTodo,this.checkTodo)this.$bus.$on(deleteTodo,this.deleteTodo)},beforeDestroy(){this.$bus.$off([checkTodo,deleteTodo])}}
/scriptstylebody {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;}
/stylesrc/components/MyItem.vue:
templatelilabelinput typecheckbox :checkedtodo.done clickhandleCheck(todo.id)/span{{todo.title}}/span/labelbutton classbtn btn-danger clickhandleDelete(todo.id,todo.title)删除/button/li
/templatescriptexport default {name:MyItem,props:[todo],methods:{handleCheck(id){this.$bus.$emit(checkTodo,id)},handleDelete(id,title){if(confirm(确定删除任务title吗)){this.$bus.$emit(deleteTodo,id)}}}}
/scriptstyle scopedli {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;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover {background-color: #eee;}li:hover button{display: block;}
/style3.11. 消息的订阅与发布 src/components/School.vue:
templatediv classschoolh2学校名称{{name}}/h2h2学校地址{{address}}/h2/div
/templatescriptimport pubsub from pubsub-jsexport default {name:School,data() {return {name:尚硅谷,address:北京,}},methods:{demo(msgName,data) {console.log(我是School组件收到了数据,data)}},mounted() {this.pubId pubsub.subscribe(demo,this.demo) //订阅消息},beforeDestroy() {pubsub.unsubscribe(this.pubId) //取消订阅}}
/scriptstyle scoped.school{background-color: skyblue;padding: 5px;}
/stylesrc/components/Student.vue:
templatediv classstudenth2学生姓名{{name}}/h2h2学生性别{{sex}}/h2button clicksendStudentName把学生名给School组件/button/div
/templatescriptimport pubsub from pubsub-jsexport default {name:Student,data() {return {name:JOJO,sex:男,}},methods: {sendStudentName(){pubsub.publish(demo,this.name) //发布消息}}}
/scriptstyle scoped.student{background-color: pink;padding: 5px;margin-top: 30px;}
/style总结 消息订阅与发布pubsub 消息订阅与发布是一种组件间通信的方式适用于任意组件间通信 使用步骤 1.安装pubsubnpm i pubsub-js 2.引入import pubsub from pubsub-js 3.接收数据A组件想接收数据则在A组件中订阅消息订阅的回调留在A组件自身
export default {methods(){demo(data){...}}...mounted() {this.pid pubsub.subscribe(xxx,this.demo)}
} 4.提供数据pubsub.publish(xxx,data) 5.最好在beforeDestroy钩子中使用pubsub.unsubscribe(pid)取消订阅
使用消息的订阅与发布优化Todo-List src/App.vue:
templatediv idrootdiv classtodo-containerdiv classtodo-wrapMyHeader addTodoaddTodo/MyList :todostodos/MyFooter :todostodos checkAllTodocheckAllTodo clearAllTodoclearAllTodo//div/div/div
/templatescriptimport pubsub from pubsub-jsimport MyHeader from ./components/MyHeader.vueimport MyList from ./components/MyList.vueimport MyFooter from ./components/MyFooter.vueexport default {name:App,components: { MyHeader,MyList,MyFooter },data() {return {todos:JSON.parse(localStorage.getItem(todos)) || []}},methods:{//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckTodo(_,id){this.todos.forEach((todo){if(todo.id id) todo.done !todo.done})},//删除一个tododeleteTodo(id){this.todos this.todos.filter(todo todo.id ! id)},//全选or取消勾选checkAllTodo(done){this.todos.forEach(todo todo.done done)},//删除已完成的todoclearAllTodo(){this.todos this.todos.filter(todo !todo.done)}},watch:{todos:{deep:true,handler(value){localStorage.setItem(todos,JSON.stringify(value))}}},mounted(){this.pubId pubsub.subscribe(checkTodo,this.checkTodo)this.$bus.$on(deleteTodo,this.deleteTodo)},beforeDestroy(){pubsub.unsubscribe(this.pubId)this.$bus.$off(deleteTodo)}}
/scriptstylebody {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;}
/stylesrc/components/myItem.vue:
templatelilabelinput typecheckbox :checkedtodo.done clickhandleCheck(todo.id)/span{{todo.title}}/span/labelbutton classbtn btn-danger clickhandleDelete(todo.id,todo.title)删除/button/li
/templatescriptimport pubsub from pubsub-jsexport default {name:MyItem,props:[todo],methods:{handleCheck(id){ pubsub.publish(checkTodo,id)},handleDelete(id,title){if(confirm(确定删除任务title吗)){this.$bus.$emit(deleteTodo,id)}}}}
/scriptstyle scopedli {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;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover {background-color: #eee;}li:hover button{display: block;}
/style3.12. $nextTick 使用$nextTick优化Todo-List src/App.vue:
templatediv idrootdiv classtodo-containerdiv classtodo-wrapMyHeader addTodoaddTodo/MyList :todostodos/MyFooter :todostodos checkAllTodocheckAllTodo clearAllTodoclearAllTodo//div/div/div
/templatescriptimport pubsub from pubsub-jsimport MyHeader from ./components/MyHeader.vueimport MyList from ./components/MyList.vueimport MyFooter from ./components/MyFooter.vueexport default {name:App,components: { MyHeader,MyList,MyFooter },data() {return {todos:JSON.parse(localStorage.getItem(todos)) || []}},methods:{//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckTodo(_,id){this.todos.forEach((todo){if(todo.id id) todo.done !todo.done})},//删除一个tododeleteTodo(id){this.todos this.todos.filter(todo todo.id ! id)},//更新一个todoupdateTodo(id,title){this.todos.forEach((todo){if(todo.id id) todo.title title})},//全选or取消勾选checkAllTodo(done){this.todos.forEach(todo todo.done done)},//删除已完成的todoclearAllTodo(){this.todos this.todos.filter(todo !todo.done)}},watch:{todos:{deep:true,handler(value){localStorage.setItem(todos,JSON.stringify(value))}}},mounted(){this.pubId pubsub.subscribe(checkTodo,this.checkTodo)this.$bus.$on(deleteTodo,this.deleteTodo)this.$bus.$on(updateTodo,this.updateTodo)},beforeDestroy(){pubsub.unsubscribe(this.pubId)this.$bus.$off(deleteTodo)this.$bus.$off(updateTodo)}}
/scriptstylebody {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: #e04e49;border: 1px solid #bd362f;}.btn-danger:hover {color: #fff;background-color: #bd362f;}.btn-info {color: #fff;background-color: rgb(50, 129, 233);border: 1px solid rgb(1, 47, 212);margin-right: 5px;}.btn-info:hover {color: #fff;background-color: rgb(1, 47, 212);}.btn:focus {outline: none;}.todo-container {width: 600px;margin: 0 auto;}.todo-container .todo-wrap {padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
/stylesrc/components/MyItem.vue:
templatelilabelinput typecheckbox :checkedtodo.done clickhandleCheck(todo.id)/span v-show!todo.isEdit{{todo.title}}/spaninput typetext v-showtodo.isEdit :valuetodo.title blurhandleBlur(todo,$event) refinputTitle/labelbutton classbtn btn-danger clickhandleDelete(todo.id,todo.title)删除/buttonbutton classbtn btn-info v-show!todo.isEdit clickhandleEdit(todo)编辑/button/li
/templatescriptimport pubsub from pubsub-jsexport default {name:MyItem,props:[todo],methods:{handleCheck(id){ pubsub.publish(checkTodo,id)},handleDelete(id,title){if(confirm(确定删除任务title吗)){this.$bus.$emit(deleteTodo,id)}},handleEdit(todo){// 如果todo自身有isEdit属性就将isEdit改成trueif(Object.prototype.hasOwnProperty.call(todo,isEdit)){todo.isEdit true}else{// 如果没有就向todo中添加一个响应式的isEdit属性并设为truethis.$set(todo,isEdit,true)}// 当Vue重新编译模板之后执行$nextTick()中的回调函数this.$nextTick(function(){// 使input框获取焦点this.$refs.inputTitle.focus()})},// 当input框失去焦点时更新handleBlur(todo,event){todo.isEdit falseif(!event.target.value.trim()) return alert(输入不能为空)this.$bus.$emit(updateTodo,todo.id,event.target.value)}}}
/scriptstyle scopedli {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;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover {background-color: #eee;}li:hover button{display: block;}
/styleTodo-List最终效果 总结 $nextTick