NuxtN
Nuxt4mo ago
8 replies
JuanMCe

useRoute inside plugin

Is this suggestion correct?
Don't call useRoute inside a Nuxt plugin setup—it's only available in component
contexts. Instead, read the route name from the router instance on the Nuxt app.
This ensures the plugin works both on server and client side.

plugin is something like this:

export default defineNuxtPlugin({
  name: 'plugin',
  parallel: true,
  setup: async () => {
    const route = useRoute();
    if (route.name === 'my-route') {
      doSomething()
    }
  },
});

if true, show sources
Was this page helpful?