Vue的Todolist改进

<body>
	<div id='app'>
		<input type="text" v-model="inputValue"/><br>
		<input type="text" v-model:lazy="inputValue"/>
		<button v-on:click="handleBtnClick">点击</button>
		<ul>
			<todo-item v-bind:content="item"
			           v-bind:index="index"
					   v-for="(item,index) in list"
					   @delete="handleItemDelete">
			</todo-item>
		</ul>
	</div>
    <script>
		// //全局组件
		// Vue.component("TodoItem", {
		// 	props:['content'],
		// 	template: "<li>{{content}}</li>",
		// })

        //局部组件
        var TodoItem = {
			props:['content','index'],
			template: "<li @click='handleItemClick'>{{content}}</li>",
			methods:{
				handleItemClick:function(){
					this.$emit("delete", this.index)  //向父组件触发事件
				}
			}
		}

    	var app = new Vue({
    		el: '#app',
			//注册组件(局部组件)
			components:{
				TodoItem: TodoItem
			},
    		data: {
    			list: [],
    			inputValue:''
    		},
    		methods: {
    			handleBtnClick: function () {
    				this.list.push(this.inputValue)
    				this.inputValue = ''
    			},
				handleItemDelete: function(index) {
					this.list.splice(index,1)
				}
    		}

    	})
    </script>
</body>
原文地址:https://www.cnblogs.com/lfnumber7/p/12543572.html