分享

Vue的Todolist改进

 头号码甲 2021-11-11
<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>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约