Access Nuxt Router in plugin

Viewed 5000

I've created a Nuxt plugin that is loaded in the config file with some global functions. In one function, I'd like to access the router, and push to a new route. I am getting an error that router is undefined. Can someone help me understand how I access the router here, if its not attached to the context.

export default (context, inject) => {
  const someFunction = () => {
    context.router.push({ name: 'route-name' } })
  }
}
2 Answers

Try to destruct the context then use app to access the router :

export default ({app}, inject) => {
  const someFunction = () => {
    app.router.push({ name: 'route-name' } })
  }
}

I figured this out. When using context, you can access the router, like this:

context.app.router
Related