相关疑难解决方法(0)

Vue.js 3 this.$root.$on 不是函数

我有一个触发this.$root.$emit('some-root-event')类似的组件

clickHandler: function() {
    this.$root.$emit("some-root-event", "aaaaaaaaaaaaaaaaaaaaaa");
    console.log('About $root.$emit')
}
Run Code Online (Sandbox Code Playgroud)

里面的另一个组件应该捕获这个事件,但抛出一个错误:

this.$root.$on 不是函数

第二个组件的代码看起来像

mounted() {
    this.$root.$on("some-root-event", (data) => {
        console.log("About listener catch $root some-root-event"); console.log(data)
    })
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我了解问题出在哪里吗?

javascript events vue.js vue-component vuejs3

5
推荐指数
1
解决办法
4785
查看次数

标签 统计

events ×1

javascript ×1

vue-component ×1

vue.js ×1

vuejs3 ×1