vue.js实例todoList项目

2020-06-12 21:26:08易采站长站整理

新建组件todoList.vue,在App.vue中引入


import TodoList from "./components/todoList";


export default {
name: 'app',
components: {
TodoList
}
}


<template>
<div id="app">
<h1>TO DO LIST !</h1>
<todo-list></todo-list>
</div>
</template>

 三处缺一不可,第一处引入文件,第二处注册组件,第三处声明组件位置

由于html中不区分大小写,所以驼峰命名方式中的大写变为-,即第三处中写成todo-list,不理解的可以动手实验一下!

todoList.vue中data如下:


data () {
return{
items:[
{
label:"homework",
finish:false
},
{
label:"run",
finish:false
},
{
label:"drink water",
finish:false
}
] }
}

 有三件事情:homework、run、drink water,通过v-for渲染:


<ul>
<li v-for="item in items">{{item.label}}</li>
</ul>

列表展示完成,现在添加点击列表,改变列表样式,标记为完成!


<ul>
<li v-for="item in items" v-on:click="done(item)" v-bind:class="{done:item.finish}">{{item.label}}</li>
</ul>

添加v-on:click,v-bind:class

v-on:click=”done(item)”表示点击执行done方法,item作为参数。

v-bind:class=”{done:item.finish}”表示,如果item.finish==true时,class=“done”。


methods:{
done:function (item) {
item.finish = !item.finish
}
}


.done{
color: red;
}