Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Nuxt Router in plugin

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' } })
  }
}
like image 715
user1666858 Avatar asked Jan 08 '21 18:01

user1666858


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' } })
  }
}
like image 142
Boussadjra Brahim Avatar answered Oct 08 '22 09:10

Boussadjra Brahim


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

context.app.router
like image 42
user1666858 Avatar answered Oct 08 '22 08:10

user1666858