Register additional hooks in NuxtJs project using Vue class components
P粉904450959
P粉904450959 2024-03-27 08:38:00
0
1
438

I'm building my first NuxtJs project and I'm also using Vue class components to write my components as classes.

After applying the Vue class component, I have issues accessing the beforeRouteEnter component hook (it is no longer called). So I found documentation on registering additional hooks when using this library, but I can't figure out where to place the import statement in the NuxtJs structure.

I have this file (same as the documentation):

// class-component-hooks.js
import Component from 'vue-class-component'

// Register the router hooks with their names
Component.registerHooks([
  'beforeRouteEnter',
  'beforeRouteLeave',
  'beforeRouteUpdate'
])

And I'd like help on how to set it up in my NuxtJs project:

// Where should I place this?
import './class-component-hooks'

P粉904450959
P粉904450959

reply all(1)
P粉879517403

The result is very simple:

I have placed the .js file into the plugins folder:

// plugins/class-component-hooks.js
import Component from 'vue-class-component'

// Register the router hooks with their names
Component.registerHooks([
  'beforeRouteEnter',
  'beforeRouteLeave',
  'beforeRouteUpdate'
])

Then in my nuxt.config.js file I placed this line:

...
  plugins: [
    { src: "~/plugins/class-component-hooks.js", mode: "client" },
  ],
...
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template