在Nuxt插件中访问Vue实例
我创建了一个 nuxt 插件来使用 simple-jsonrpc-js 包以及nuxt auth 插件。现在我可以使用 this.$jsonApi.call()
访问它,以使用 nuxt/auth 提供的令牌向服务器执行请求。
我在 nuxt 应用程序的许多组件中使用它,因此我想通过将 _jrpc.call()
包装在 try-catch 块中来为我的插件创建一个错误处理程序。因为我使用 Vuetify 我想显示一个包含错误消息内容的小吃栏组件。目前我的代码如下所示:
async call<T = Response<any>>(method: string, data: object = {}): Promise<T> {
const token = this.accessTokenFactory()
try {
return await this._jrpc.call(method, {
token: token,
data
})
} catch (e:any) {
const ComponentClass = Vue.extend(VSnackbar)
const instance = new ComponentClass()
instance.$slots.default = [e.message]
instance.$mount()
document.querySelector('body')?.appendChild(instance.$el)
}
}
但是当我从 api 收到错误时,我从 catch 块
来自 vuetify 组件代码:
const {
bar,
bottom,
footer,
insetFooter,
left,
right,
top,
} = this.$vuetify.application
我的猜测是我的 Vue 实例使用不有 this.$vuetify.application
因为它与 nuxt 应用程序使用的 Vue 实例不同...
这就是为什么我问:在注入 a 时如何访问 Nuxt 中的相同 Vue 实例插件? Nuxt 文档没有帮助,我还没有找到类似的问题。
预先感谢您的帮助
i created a nuxt plugin to use the simple-jsonrpc-js package together with the nuxt auth plugin. Now i can access it with this.$jsonApi.call()
to perform a request to the server with my token provided by nuxt/auth.
I use it in many components within my nuxt app so i wanted to create an error handler for my plugin by wrapping _jrpc.call()
in a try-catch block. Because im using Vuetify i want to show a snackbar component with the content of the error Message. Currently my code looks like this:
async call<T = Response<any>>(method: string, data: object = {}): Promise<T> {
const token = this.accessTokenFactory()
try {
return await this._jrpc.call(method, {
token: token,
data
})
} catch (e:any) {
const ComponentClass = Vue.extend(VSnackbar)
const instance = new ComponentClass()
instance.$slots.default = [e.message]
instance.$mount()
document.querySelector('body')?.appendChild(instance.$el)
}
}
But when i get an error from my api, i get this error from the catch block
Which comes from the vuetify component code:
const {
bar,
bottom,
footer,
insetFooter,
left,
right,
top,
} = this.$vuetify.application
My guess is that the Vue instance i am using doesn't have the this.$vuetify.application
becuase its not the same as the Vue instance used by the nuxt application...
That's why im asking: how do i access the same Vue instance in Nuxt when injecting a plugin? The Nuxt docs arent helping and i haven't found a similar question.
Thanks in advance for helping
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
刚刚找到了这个问题的答案!我也已经尝试寻找这个有一段时间了。
您可以从客户端通过
window.$nuxt
访问 Nuxt 使用的根 Vue 实例(与 Vue 组件内的this.$root
相同)。对于您的代码,请在调用代码上使用
window.$nuxt.$vuetify.application
。注意:如果您在插件创建本身上使用此功能,请确保首先通过执行 setTimeout、挂钩模块或添加组件事件来等待 Nuxt 实例创建,因为 Nuxt 实例尚未创建尚未创建。
编辑:修正了句子措辞并添加了一些见解。
Found the answer to this just now! I've been try to search for this for quite some time now as well.
You can access the root Vue instance used by Nuxt from the client as
window.$nuxt
(which is the same asthis.$root
inside the Vue components).For your code, use
window.$nuxt.$vuetify.application
on the calling code.NOTE: If you are using this on the plugin creation itself, make sure to wait for the Nuxt instance to be created first, by either doing a setTimeout, hooking through a module, or adding a component event, because the Nuxt instance has not been created yet.
EDIT: Fixed the sentence wording and added some insight.
目前我还没有找到解决方案,但在本文中找到了替代方案
如何使用 Nuxt、Vuetify 和 Vuex 创建全局小吃栏。
恕我直言,自从监听组件以来,这并不像插件那样“干净”位于
/layouts/default
而不是 nuxt/vue 上下文中,因此需要将其添加到所有布局中才能工作。我想解决方法是“基本”布局,所有其他布局都需要从中继承
Currently i haven't found a solution but an alternative in this article
How to create a global snackbar using Nuxt, Vuetify and Vuex.
IMHO this isn't as "clean" as a plugin since the listening component sits in
/layouts/default
rather than the nuxt/vue context and therefore needs to be added it in all layouts to work.I guess a workaround would be a "base" layout from which the all other layouts need to inherit