javascript - 无法读取未定义的属性“$nuxt”
问题描述
我正在处理项目并在尝试从插件访问事件时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);
});
},
}
所以,我的问题是“如何在插件中监听/捕获事件”?
解决方案
您可以在 nuxt 插件的上下文中引用应用程序。文档https://nuxtjs.org/api/context/
import Vue from 'vue';
export default ({ app }) => {
app.$on('emit-height', (payload) => {
Vue.prototype.$bannerHeight = payload;
});
}
推荐阅读
- android - 如何使用 Runnable 和布尔标志自动/定期滚动 recyclerView
- ios - 导航栏的右键不见了
- django - 使用 Django/Postgres 获取一个计算字段,如果不存在另一个字段值,则获取一个字段值
- ios - 我无法从 NSArray(contentsOfFile: ) 获取数据,它返回 nil
- r - 在包 recharts 中找不到函数 echartR
- c# - 是否可以一次阻止多个设备同步到 Google 云端硬盘?
- javascript - 响应时字体大小不同
- python - 如何使用另一个数组更改数组的内容?
- node.js - 将 XLSX 文件存储在 CosmosDB 中并能够将结果分块发送回的最佳方法?
- c++ - 我将如何在 C++ 中实现处理器调度模拟器?