admin管理员组

文章数量:1203210

Nuxt 2.15.6; I want to switch the layout of my menu component by dynamically switching menu components in my root layout.

default.vue

<template>
  <component :is="navLayout"></component>
  <Nuxt :navLayout="navLayout = $event" />
</template>
data() {
  return {
    navLayout: "default"
  };
},

In the "child" components of , my pages eg. login.vue (/login) I $emit an event;

...
import nav2 from "@/layouts/nav2";
...
created() {
  this.$emit("navLayout", nav2);
},

Now it seems to be the <Nuxt> component is not able to catch the event. I also tried calling a <Nuxt @navLayout="test()" /> method.

How can I avoid this.$root.$emit(...); in my login.vue and

this.$root.$on("navLayout", navLayout => {
  this.navLayout = navLayout;
});

in default.vue?

Nuxt 2.15.6; I want to switch the layout of my menu component by dynamically switching menu components in my root layout.

default.vue

<template>
  <component :is="navLayout"></component>
  <Nuxt :navLayout="navLayout = $event" />
</template>
data() {
  return {
    navLayout: "default"
  };
},

In the "child" components of , my pages eg. login.vue (/login) I $emit an event;

...
import nav2 from "@/layouts/nav2";
...
created() {
  this.$emit("navLayout", nav2);
},

Now it seems to be the <Nuxt> component is not able to catch the event. I also tried calling a <Nuxt @navLayout="test()" /> method.

How can I avoid this.$root.$emit(...); in my login.vue and

this.$root.$on("navLayout", navLayout => {
  this.navLayout = navLayout;
});

in default.vue?

Share Improve this question edited Jul 1, 2024 at 9:32 kissu 46.6k16 gold badges89 silver badges186 bronze badges asked Jun 3, 2021 at 7:16 nonNumericalFloatnonNumericalFloat 1,7672 gold badges20 silver badges37 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 17

EDIT: This answer works fine as it looks like you cannot do it right now: https://github.com/nuxt/nuxt.js/issues/8122#issuecomment-709443008

In the child component

<button @click="$nuxt.$emit('eventName', 'nice payload')">nice</button>

On the default layout

<script>
export default {
  created() {
    this.$nuxt.$on('eventName', ($event) => this.test($event))
  },
  methods: {
    test(e) {
      console.log('test ok >>', e)
    },
  },
}
</script>

Putting a listener on Nuxt itself does not work.

<Nuxt @navLayout="navLayout = $event" :navLayout="navLayout" />

I can see the event go and the listener plugged to <nuxt></nuxt> but it does not trigger any method with the listener...

PS: works for <nuxt-child></nuxt-child> at least.

Maybe I don't understand your question correctly, but it seems to me that you are trying to do yourself what layouts are meant to do. This would mean that you would create a layout with the menu component for login, a default layout etc. like this:

login.vue

<template>
  <LoginMenu>
  <Nuxt/>
</template>

default.vue

<template>
  <DefaultMenu>
  <Nuxt/>
</template>

On your page you would do:

export default {
   layout: login
}

And then that would load the layout with the login menu component. On all other pages it would load the default menu.

More info here: https://nuxtjs.org/examples/layouts/

本文标签: javascriptHow to listen for a page emit in a nuxt layoutStack Overflow