1.2 组件基础及组件注册
// js设置 Vue.component('todo-item', { // 属性声明 props: { title: String, // 直接定义类型,没有默认值 del: { type: Boolean, // 定义类型 default: false // 默认值 }, }, // 绑定对象 data: function() { return {} }, // 方法 methods: { }, // 模板 template: ` <li> <span v-if="!del">{{title}}</span> <span v-else style="text-decoration: line-through;">{{title}}</span> <button v-show="!del">删除</button> </li> ` }) // html调用传参 <ul> <todo-item v-for="item in list" :title="item.title" :del="item.del"></todo-item> </ul>// js定义组件 Vue.component('todo-list', { template: ` <ul> <todo-item v-for="item in list" :title="item.title" :del="item.del"></todo-item> </ul> `, data: function() { return { list: [{ title: '课程1', del: false, },{ title: '课程2', del: true, }] } } }) // html <todo-list></todo-list>
Last updated