采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
data:function(){ return{ number:this.count //this无法指向count对象?????? } },
var counter = { props: ['count'], template: '<div @click="handle">{{number}}</div>', data: function () { return { number: this.count, }; }, methods: { handle: function () { this.number += 2; this.$emit('inc', 2); }, }, };
按照你的逻辑,应该是这样吧,就是在组件里面应该是number,不是count,从父组件传来的值是不可以修改的,所以template里面应该是number,你再试试
应该是的
data里面是用来定义数据的,贴下代码,看下count是哪里的
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>nana</title> <script src="./vue.js"></script> </head> <body> <div id="app"> <counter :count="0" @inc="handlel"></counter> <counter :count="1" @inc="handlel"></counter> <div>{{total}}</div> </div> <script> var counter={ props:['count'], template:'<div @click="handle">{{count}}</div>', data:function(){ return{ number:this.count } }, methods:{handle:function(){ this.number+=2; this.$emit('inc',2) } } } var vm=new Vue({ el:"#app", data:{ total:5 }, components:{ counter:counter }, methods:{handlel:function(step){ this.total+=step }} }) </script> </body> </html> //谢谢了
登录后可查看更多问答,登录/注册
课程紧跟Vue3版本迭代,企业主流版本Vue2+Vue3全掌握
1.8k 20
1.5k 19
2.6k 17
1.3k 16
1.5k 15