问答详情
源自:3-4 实现todolist的删除功能

我定义的子组件是局部组件,为什么<todo-item @click='itemDelete'></to-item>没报错,但是点击,itemDelete不执行

<html>

    <head>

        <meta charset="utf-8">

        <title>属性绑定和双向数据绑定</title>

        <script src="./vue.js"></script>

    </head>

    <body>

        <div id="root">

           <div>

                <input type="text" v-model="pushvalue">

                <button @click="clickButton">提交</button> 

            </div>

            <ul>

                <todo-item v-for="(item,index) of list"

                :key="index"

                :content="item"

                :index="index"

                @click='itemDelete'

                >

                </todo-item>

            </ul>

        </div>

        <script>


           // Vue.component("todo-item",{

                //template:'<li>item</li>'

           // })


            var todoItem={

                template:'<li @click="listClick">{{content}}</li>',

                props:['content','index'],

                methods:{

                    listClick:function(){

                        this.$emit("delete",this.index);

                    }

                }

            }


            new Vue({

                el:"#root",

                components:{

                    'todo-item':todoItem

                },

                data:{

                    list:[],

                    pushvalue:''

                },

                methods:{

                    clickButton:function(){

                        if(this.pushvalue==''){

                            alert("请输入再提交");

                        }

                        else{

                            this.list.push(this.pushvalue);

                            this.pushvalue='';

                        }

                   

                    },

                    itemDelete:function(index){

                        alert(index);

                        this.list.splice(index,1);

                        alert(index);

                    }

                }

            })

        </script>

    </body>

</html>


提问者:宝慕林0444165 2019-05-10 15:11

个回答

  • 阳光下的老牛
    2021-01-08 17:31:24

    <todo-item>其实是一个无法识别的标签,可以理解为自定义标签,但你没有定义它

  • 宝慕林0444165
    2019-05-10 15:32:46

    子组件对外发布的是delete,我监听的是click,有反应才怪,谢谢各位