【问题标题】:Cannot read property '$nuxt' of undefined无法读取未定义的属性“$nuxt”
【发布时间】:2019-05-22 22:09:15
【问题描述】:

我正在处理 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);
    });
  },
}

那么,我的问题是“如何在插件中监听/捕获事件”?

【问题讨论】:

    标签: javascript vue.js nuxt.js


    【解决方案1】:

    您可以在 nuxt 插件的上下文中引用应用程序。文档https://nuxtjs.org/api/context/

    import Vue from 'vue';
    
    export default ({ app }) => {
      app.$on('emit-height', (payload) => {
        Vue.prototype.$bannerHeight = payload;
      });
    }
    

    【讨论】:

    • 这还能用吗?现在似乎无法正常工作。
    猜你喜欢
    • 2019-03-27
    • 2021-07-08
    • 2022-08-15
    • 2021-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多