引言:
这一节用Vue做了一个Todolist的小实例,
并且给他拆成了组件
还有简单的组件间传值该怎么办呢?
加油加油!!
##Vue做一个TodoList
做一个TodoList的完整功能,来复习的知识
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| <body> <div id="root"> <div> <input v-model="inputValue"/>
<button @click="handleSubmit">提交</button> </div> <ul> <li v-for="(item,index) of list" :key="index"> {{item}} </li> </ul> </div> <script> new Vue({ el: "#root", data: { inputValue: '', list: [], }, methods: { handleSubmit: function () { this.list.push(this.inputValue); this.inputValue = ''; } } }) </script> </body>
|
接下来我们对它进行组件拆分
定义局部的组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| <div id="root"> <div> <input v-model="inputValue"/> <button @click="handleSubmit">提交</button> </div> <ul> <todo-item v-for="(item,index) of list" :key="index" :content="item" ></todo-item> </ul>
</div> <script> let TodoItem = { props:['content'], template: '<li>{{content}}</li>' }; new Vue({ el: "#root", data: { inputValue: '', list: [], }, methods: { handleSubmit: function () { this.list.push(this.inputValue); this.inputValue = ''; } }, components: { 'todo-item': TodoItem }, }) </script> </body> </html>
|
定义全局的组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| <body> <div id="root"> <div> <input v-model="inputValue"/> <button @click="handleSubmit">提交</button> </div> <ul> <li v-for="(item,index) of list" :key="index">{{item}}</li> </ul> <ul> <todo-item></todo-item> </ul>
</div> <script> Vue.component('todo-item',{ template: '<li>全局组件</li>' });
new Vue({ el: "#root", methods: { handleSubmit: function () { this.list.push(this.inputValue); this.inputValue = ''; } } }) </script> </body>
|
把li标签拆成了组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <body> <div id="root"> <div> <input v-model="inputValue"/> <button @click="handleSubmit">提交</button> </div> <ul> <todo-item v-for="(item,index) of list" :key="index" :content="item" ></todo-item>
</ul>
</div> <script> Vue.component('todo-item',{ props:['content'], template: '<li>{{content}}</li>' });
new Vue({ el: "#root", data: { inputValue: '', list: [], }, methods: { handleSubmit: function () { this.list.push(this.inputValue); this.inputValue = ''; } } }) </script> </body>
|
其实每一个Vue的实例都是一个组件,而当你不输入template的内容时,它默认会是挂载点下的所有内容作为其模板
1 2 3 4 5 6 7 8 9 10
| Vue.component('todo-item', { props: ['content'], template: '<li @click="handleClick">{{content}}</li>', methods: { handleClick:function () { alert('clicked') } } });
|
我们再添加一个删除功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| <body> <div id="root"> <div> <input v-model="inputValue"/> <button @click="handleSubmit">提交</button> </div> <ul> <todo-item v-for="(item,index) of list" :key="index" :content="item" :index="index" @delete="handleDelete" ></todo-item> </ul> </div> <script> Vue.component('todo-item', { props: ['content', 'index'], template: '<li @click="handleClick">{{content}}</li>', methods: { handleClick: function () { this.$emit('delete', this.index) }, } }); new Vue({ el: "#root", data: { inputValue: '', list: [], }, methods: { handleSubmit: function () { this.list.push(this.inputValue); this.inputValue = ''; }, handleDelete: function (index) { this.list.splice(index, 1) } } }) </script>
|
总结一下简单的组件间传值
- 在子组件上用v-bind来对子组件传值
- 子组件填入
props
的属性,接收传来的值,就可以使用了
- 使用
this.$emit()
这个函数
- 子组件要监听emit所发出的事件名并对其有相应的反应