我正在嘗試從 nuxt 中的實體屬性創建一個事件,但是該事件未發出或接收。
plugins/internal/bus.js
import Vue from 'vue';
const eventBus = {}
eventBus.install = function (Vue) {
Vue.prototype.$bus = new Vue()
}
Vue.use(eventBus)
plugins/internal/index.js
import Vue from 'vue';
export default function (_ctx, inject) {
const notify = function (msg) {
console.log('emitting', msg);
setInterval(() => {
this.$bus.$emit('add', msg);
}, 500);
}
.bind(new Vue());
inject('notify', notify);
}
nuxt.config.js
plugins: [
'~/plugins/internal/bus.js',
'~/plugins/internal/index.js',
...
]
在我的組件中,
mounted() {
this.$bus.$on('add', (val) => {
console.log(val);
})
this.$bus.$on('close', this.onClose)
},
Doing this.$notify({ foo: 'bar' }), calls the instance property correctly, however either the event is not emitted or is not received, frankly not sure how to debug this. What am I missing here?
uj5u.com熱心網友回復:
最后,問題來自Notification未正確注冊的組件 ()。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/446454.html
標籤:javascript vue.js events vuejs2 nuxt.js
上一篇:添加新行以回傳內部方法
