vue 中$on()的this问题

Vue.prototype.bus=new Vue();

    Vue.component('child',{
        template:`<div @click='handleClick'>{{text}}</div>`,
        props:{
            content:[String,Number]
        },        data(){            return {
                text:this.content
            }
        },
        methods:{
            handleClick(){                this.bus.$emit('change',this.text)
            }
        },
        mounted(){            var _this=this
            console.log(_this);            this.bus.$on('change',function (msg) {
                    _this.text=msg
                    console.log(this);
            })
        }
    })    var vm = new Vue({
        el:"#app",
    })

上面代码中的 this.bus.$on('change',function (msg) {

                    _this.text=msg
                    console.log(this);
            })
        }

里面的回调函数里面的this到底是指向谁?希望大佬帮忙解答一下,谢谢


吃鸡游戏
浏览 3264回答 3
3回答

无尘粉笔

this指向有问题

MYYA

Vue.prototype.bus&nbsp;=&nbsp;new&nbsp;Vue();&nbsp;//&nbsp;<-&nbsp;指向这个实例//&nbsp;也就是说&nbsp;this.bus.$on('change',function&nbsp;(msg)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;_this.text=msg&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;console.log(this&nbsp;===&nbsp;_this.bus);&nbsp;//&nbsp;true &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;})
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Vue.js