Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit event from page to layout #8122

Closed
steveDL opened this issue Sep 27, 2020 · 14 comments
Closed

Emit event from page to layout #8122

steveDL opened this issue Sep 27, 2020 · 14 comments

Comments

@steveDL
Copy link

steveDL commented Sep 27, 2020

Is it possible to emit an event from a page back up to default.vue layout?

<nuxt @loading="loadingHandler" />

@ontoneio
Copy link

ontoneio commented Oct 3, 2020

@steveDL Can you clarify what the use case for this might be?

Just off top I'd imagine that there might be a different way to achieve the end goal you are looking for.
Do you have an example?

@tbrannt
Copy link

tbrannt commented Oct 15, 2020

I actually tried to do this and found that the global event bus was actually what I was looking for.

Simply do

this.$nuxt.$emit('eventName')

in any component (as opposed to this.$emit('eventName'))

and listen to it in any component (and also in your default.vue layout) with

created () {
  this.$nuxt.$on('eventName', () => {
    this.animateSecureHint = true
  })
}

@stale
Copy link

stale bot commented Nov 15, 2020

Thanks for your contribution to Nuxt.js!
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
If you would like this issue to remain open:

  1. Verify that you can still reproduce the issue in the latest version of nuxt-edge
  2. Comment the steps to reproduce it

Issues that are labeled as pending will not be automatically marked as stale.

@stale stale bot added the stale label Nov 15, 2020
@stale stale bot closed this as completed Nov 26, 2020
@drewbaker
Copy link

This is amazing. Nuxt has a global event bus?! I didn't know this, only been using Nuxt for 3 years...

@ichaiwut
Copy link

This is awesome! thanks @tbrannt ! 🙏

@kissu
Copy link

kissu commented Jun 3, 2021

It's strange that this works with <nuxt-child></nuxt-child> but not <nuxt></nuxt>.

I can even see the listener on <nuxt></nuxt> and the event emitted from the child but the method does not trigger.

This solution may be good enough tho, thanks !

@oluudeh
Copy link

oluudeh commented Aug 23, 2021

I actually tried to do this and found that the global event bus was actually what I was looking for.

Simply do

this.$nuxt.$emit('eventName')

in any component (as opposed to this.$emit('eventName'))

and listen to it in any component (and also in your default.vue layout) with

created () {
  this.$nuxt.$on('eventName', () => {
    this.animateSecureHint = true
  })
}

Is there a way to do this using @nuxtjs/composition-api?

@Yvanov-N
Copy link

Yvanov-N commented Oct 5, 2021

Thanks, Helpfull !

@ibrahimBeladi
Copy link

How could this be tackled with @nuxtjs/composition-api?

@viandwi24
Copy link

How could this be tackled with @nuxtjs/composition-api?

I'm not very good at typescript and nuxt, but you can make this reference

  • composables/use-nuxt.ts
import { getCurrentInstance } from '@nuxtjs/composition-api'

export const useNuxt = () => {
  const instance = getCurrentInstance()
  return (instance as { proxy: Vue }).proxy.$nuxt
}
  • parent.vue
import { useNuxt } from '~/composables/use-nuxt'
const $nuxt = useNuxt()
onMounted(() => $nuxt.$on('test', () => alert('test')))
  • child.vue
import { useNuxt } from '~/composables/use-nuxt'
const $nuxt = useNuxt()
$nuxt.$emit('test')

@hinst
Copy link

hinst commented Dec 7, 2022

I tried the approach from the latest comment but it does not work anymore:

function $nuxt.emit() exists, but function $nuxt.on() does not exist in my latest Nuxt 3 installation.

I find it inconvenient overall, that existing functions get removed from the new version of the framework. This approach creates headache for the users of the framework

@viandwi24
Copy link

I tried the approach from the latest comment but it does not work anymore:

function $nuxt.emit() exists, but function $nuxt.on() does not exist in my latest Nuxt 3 installation.

I find it inconvenient overall, that existing functions get removed from the new version of the framework. This approach creates headache for the users of the framework

what version did you try it on?

@madhusudanbabar
Copy link

I tried the approach from the latest comment but it does not work anymore:
function $nuxt.emit() exists, but function $nuxt.on() does not exist in my latest Nuxt 3 installation.
I find it inconvenient overall, that existing functions get removed from the new version of the framework. This approach creates headache for the users of the framework

what version did you try it on?

Same here, I'm also unable to access $nuxt.$on, getting this error TypeError: $nuxt.$on is not a function at default.vue:90:13
I'm using "nuxt": "^3.6.3"

@Hebilicious
Copy link
Member

Hebilicious commented Sep 24, 2023

The global event bus got removed in vue 3, here's the migration guide :
https://v3-migration.vuejs.org/breaking-changes/events-api.html
In most cases this is not what you want, as vue docs say :

In most circumstances, using a global event bus for communicating between components is discouraged. While it is often the simplest solution in the short term, it almost invariably proves to be a maintenance headache in the long term. Depending on the circumstances, there are various alternatives to using an event bus:

  • Props and events should be your first choice for parent-child communication. Siblings can communicate via their parent.
  • Provide / inject allow a component to communicate with its slot contents. This is useful for tightly-coupled components that are always used together.
  • Provide / inject can also be used for long-distance communication between components. It can help to avoid 'prop drilling', where props need to be passed down through many levels of components that don't need those props themselves.
  • Prop drilling can also be avoided by refactoring to use slots. If an interim component doesn't need the props then it might indicate a problem with separation of concerns. Introducing a slot in that component allows the parent to create the content directly, so that props can be passed without the interim component needing to get involved.
  • Global state management, such as Pinia.

If you really need to implement something with an event bus, I would recommend mitt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests