Vue实现todolist的删除功能

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
</head>
<body>
    <div id = "root">
        <div> 
            <input v-model = "inputvalue"/>
            <button @click = "handleclick">提交</button>
        </div>
        <ul>
            <!-- <li v-for = "(item, index) of list" :key = "index">{{item}}</li> -->
            <todoitem 
                v-for = "(item, index) of list" 
                :key = "index" 
                :content = "item"
                :index = "index"
                @delete = "handledelete"
                >
            </todoitem>
        </ul>
    </div>
    <script>
        // 全剧组件
        Vue.component('todoitem',{
            //声明数据接收
            props: ['content', 'index'],
            template: '<li @click = "myclick">{{content}}{{index}}</li>',
            methods:{
                myclick: function(){
                    this.$emit('delete', this.index)
                }
            }
        
        } )

        new Vue({
            el: "#root",
            data: {
                inputvalue: "hello",
                list: []
            },
            methods:{
                handleclick: function(){
                    this.list.push(this.inputvalue)
                    this.inputvalue = ""
                },
                handledelete: function(index){
                    this.list.splice(index, 1)
                }
            }
        })
    </script>
</body>
</html>

父组件通过属性的形式向子组件传递参数

:content = "item"
:index = "index"
props: ['content', 'index']

子组件通过发布订阅模式向父组件传递数据

@delete = "handledelete"
 methods:{
     myclick: function(){
         this.$emit('delete', this.index)
     }
 }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面是一个简单的 Vue 实现 todolist 的添加删除功能的代码示例: ```html <!DOCTYPE html> <html> <head> <title>Vue TodoList</title> <script src="https://cdn.jsdelivr.net/npm/vue"></script> </head> <body> <div id="app"> <h1>TodoList</h1> <input type="text" v-model="newTodo"> <button @click="addTodo">Add</button> <ul> <li v-for="(todo, index) in todos"> {{ todo }} <button @click="removeTodo(index)">Delete</button> </li> </ul> </div> <script> var app = new Vue({ el: '#app', data: { newTodo: '', todos: [] }, methods: { addTodo: function() { if (this.newTodo != '') { this.todos.push(this.newTodo); this.newTodo = ''; } }, removeTodo: function(index) { this.todos.splice(index, 1); } } }) </script> </body> </html> ``` 在这个示例中,我们首先定义了一个包含两个属性的 Vue 实例,分别是 `newTodo` 和 `todos`。`newTodo` 表示用户输入的待办事项,`todos` 表示所有已添加的待办事项列表。 然后,我们定义了两个方法,`addTodo` 和 `removeTodo`,来实现添加和删除待办事项的功能。在 `addTodo` 方法中,我们首先判断用户输入的待办事项是否为空,如果不为空,则将其添加到 `todos` 列表中,并清空 `newTodo`。在 `removeTodo` 方法中,我们通过 `splice` 方法从 `todos` 列表中删除指定索引的待办事项。 最后,我们在页面中使用 `v-model` 指令绑定用户输入的待办事项到 `newTodo` 属性上,使用 `v-for` 指令循环渲染 `todos` 列表,并在每个待办事项后面添加一个删除按钮,通过 `@click` 绑定 `removeTodo` 方法来实现删除功能
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值