我正在研究nuxt.js
项目并在尝试从插件访问事件时收到错误Cannot read property '$nuxt' of undefined
。
在~/plugins/myPlugin.js
import Vue from 'vue';
this.$nuxt.$on('emit-height', (payload) => {
Vue.prototype.$bannerHeight = payload;
});
在~/plugins/nuxt.config.js
进口
plugins: [
'~/plugins/get-main-banner-height.js',
]
如果我在任何组件中使用this.$nuxt.$on
,但如上所述在插件中不起作用,则methods: {
getMainBannerHeight() {
this.$nextTick(() => {
this.$nuxt.$emit('emit-main-banner-height', this.bannerHeight);
});
},
}
可以正常工作。
在我的组件中,我正在发出高度。
https://nuxtjs.org/api/context/
所以,我的问题是“如何在插件中监听/捕捉事件”?
您可以在nuxt插件的上下文中引用app。文件import Vue from 'vue';
export default ({ app }) => {
app.$on('emit-height', (payload) => {
Vue.prototype.$bannerHeight = payload;
});
}
qazxswpoi