无法读取未定义的属性'$ nuxt'

问题描述 投票:0回答:1

我正在研究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/

所以,我的问题是“如何在插件中监听/捕捉事件”?

javascript vue.js nuxt.js
1个回答
1
投票

您可以在nuxt插件的上下文中引用app。文件import Vue from 'vue'; export default ({ app }) => { app.$on('emit-height', (payload) => { Vue.prototype.$bannerHeight = payload; }); }

qazxswpoi
© www.soinside.com 2019 - 2024. All rights reserved.