Access VueRouter outside Vue components

You've defined your router with this statement

const router = new VueRouter({
    mode: 'history',
    routes: routes
})

So all you need to do to access it outside Vue is use

router.push(...)

Or whatever else you want to do.

If you want to use it in some other file, you may need to expose it on the window.


I've used it only from components but you could try following in case you're using common boilerplate where routes are defined under router/index.js then you just import it like:

import Router from '../router'; 

After it is possible to use it in code e.g.

Router.push('/mypage')

Not sure if it works but give it a try.


I resolved this by exporting my router instead of my routes in routes.js (now router.js)

export default new VueRouter({
    mode: 'history',
    routes: routes
})

Any file can then access the router with

import router from 'config/router'