请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

v-bind:key=‘index’报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<body>
        <div id="app">
            <input type="text" name="" id="" value="" v-model="inputvalue"/>
            <button type="button" v-on:click="handlebtn">提交</button>
            <ul>
                 
                <todo-item v-for="(item,index) of list" 
                v-bind:key="index" 
                v-bind:content="item" 
                v-on:delete="handleItemDel">
                </todo-item>
                 
            </ul>
        </div>
         
         
         
         
        <script type="text/javascript">
            // Vue.component("TodoItem",{
            //     props:['content'],
            //     template:"<li>{{content}}</li>"
            // })
            var TodoItem = {
                props:['content','key'],
                template:"<li @click='handleItembtn'>{{content}}</li>",
                methods:{
                    handleItembtn: function(key){
                        this.$emit("delete",this.key)
                    }
                }
            }
             
            var app =new Vue({
                el:"#app",
                components:{
                    TodoItem: TodoItem,
                },
                data:{
                    list:[],
                    inputvalue:'',
                },
                methods:{
                    handlebtn:function(){
                        // this.$data.list.push(this.$data.inputvalue);
                        this.list.push(this.inputvalue);
                         
                        this.$data.inputvalue = '';
                    },
                    handleItemDel:function(key){
                        this.list.splice(key,1)
                    }
                },
                 
            })
        </script>
    </body>

我把视频里面的index换成了key,最后功能可以实现,但是浏览器报错,

vue.js:634 [Vue warn]: "key" is a reserved attribute and cannot be used as component prop.

求老师解答

正在回答

插入代码

1回答

因为在循环里面,本身就需要绑定一个key,但是你传参的时候,有个参数也是key,这两个重名报错

0 回复 有任何疑惑可以回复我~
  • 提问者 溯白 #1
    那为什么老师循环里面绑定的时候是index,后面传参的时候用的也是index就没报错
    回复 有任何疑惑可以回复我~ 2019-11-09 17:45:42
  • 呀呀呀亚歌 回复 提问者 溯白 #2
    因为index不是v-for里面所学要绑定的那个关键字,v-for要求绑定key,但是你想象的key是用于传参
    回复 有任何疑惑可以回复我~ 2019-11-10 19:07:03
  • 提问者 溯白 回复 呀呀呀亚歌 #3
    我懂了,因为key是vue规定的名字,不能随便用,谢谢你
    回复 有任何疑惑可以回复我~ 2019-11-14 10:08:21
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号