请稍等 ...
×

采纳答案成功!

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

我照着老师的视频跟着写了一遍代码,两个child,点击上面的dell下面的跟着改变,但是点击下面的lee上面的dell不会变

正在回答 回答被采纳积分+3

2回答

提问者 慕移动1599698 2018-05-10 20:00:33

<!DOCTYPE html>
<html>
<head lang="en">
   <meta charset="UTF-8">
   <title></title>
   <script src="vue.min.js"></script>
</head>
<body>
<!--Bus/总线机制/发布订阅模式/观察者模式-->
<div id="root">
   <child content="dell"></child>
   <child content="hello"></child>
</div>
<script>
   Vue.prototype.bus=new Vue()
   Vue.component('child',{
       props:{
           content:String
},
       data:function(){
           return {
               selfContent:this.content
}
       },
       template:'<div @click="handleClick">{{selfContent}}</div>',
       methods:{
           handleClick:function(){
               this.bus.$emit('changed',this.selfContent)
           }
       },
       mounted:function(){
           This=this;
           this.bus.$on('changed',function(value){
               This.selfContent=value;
               console.log(value)
           })
       }
   })
   var vm=new Vue({
       el:"#root"
})
</script>
</body>
</html>

0 回复 有任何疑惑可以回复我~
weibo_落叶code_0 2018-05-10 07:55:57

代码贴出来看看

0 回复 有任何疑惑可以回复我~
  • 提问者 慕移动1599698 #1
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="vue.min.js"></script>
    </head>
    <body>
    <!--Bus/总线机制/发布订阅模式/观察者模式-->
    <div id="root">
        <child content="dell"></child>
        <child content="hello"></child>
    </div>
    <script>
        Vue.prototype.bus=new Vue()
        Vue.component('child',{
            props:{
                content:String
            },
            data:function(){
                return {
                    selfContent:this.content
                }
            },
            template:'<div @click="handleClick">{{selfContent}}</div>',
            methods:{
                handleClick:function(){
                    this.bus.$emit('changed',this.selfContent)
                }
            },
            mounted:function(){
                This=this;
                this.bus.$on('changed',function(value){
                    This.selfContent=value;
                    console.log(value)
                })
            }
        })
        var vm=new Vue({
            el:"#root"
        })
    </script>
    </body>
    </html>
    回复 有任何疑惑可以回复我~ 2018-05-10 20:17:08
  • qq__9987 回复 提问者 慕移动1599698 #2
    你This=this,这里写错了,这样写是全局变量,应该是var This=this;
    回复 有任何疑惑可以回复我~ 2018-05-21 13:02:32
  • 提问者 慕移动1599698 回复 qq__9987 #3
    谢谢 谢谢
    回复 有任何疑惑可以回复我~ 2018-06-25 21:55:27
问题已解决,确定采纳
还有疑问,暂不采纳
微信客服

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

帮助反馈 APP下载

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

公众号

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