为了账号安全,请及时绑定邮箱和手机立即绑定

我定义的子组件是局部组件,为什么<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>


正在回答

2 回答

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

0 回复 有任何疑惑可以回复我~

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

0 回复 有任何疑惑可以回复我~

举报

0/150
提交
取消

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

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信