你如何从nuxt插件发出事件? [英] How do you emit an event from a nuxt plugin?

查看:48
本文介绍了你如何从nuxt插件发出事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个插件,它将发出由套接字触发的基本 nuxt 事件.然后将收到 nuxt 事件并打开小吃店.在组件内部时,使用 $nuxt

I am creating a plugin that will emit basic nuxt events triggered by sockets. The nuxt event will then be recieved and open a snackbar. When inside a component it is easy to send and receive events by using $nuxt

this.$nuxt.$on('open-snackbar', this.handler)

this.$nuxt.$emit('open-snackbar', options)

但是我如何尝试在插件中执行此操作,因此它不绑定到任何页面,而是存在于整个应用程序中.我似乎无法弄清楚如何从所述插件中发出它:

However how I'm trying to do this in a plugin so its not tied to any one page, but exists throughout the app. I can't seem to figure out how to emit it from said plugin:

export default (context) => {
    console.log(context)
    console.log(context.$emit)
    console.log(context.emit)
    console.log(context.$nuxt)
    console.log(context.app.emit)
    console.log(context.app.$nuxt)
}

context.app 似乎是正确的对象,但似乎不起作用.有什么想法吗?

context.app seems like it would be the correct object but it doesn't seem to work. Any ideas?

推荐答案

你可以在你的 plugin.js 文件中使用类似的东西,插件会使用 window.$nuxt.$emit 在客户端可用

You can use something like this in your plugin.js file, what the plugin will do is use window.$nuxt.$emit which is available on the client side

export default function (context) {
    $nuxt.$emit('event-to-emit')
}

这篇关于你如何从nuxt插件发出事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆