Vue-router: How to access $router in common js module?

Created on 25 Jul 2017  路  3Comments  路  Source: vuejs/vue-router


I have a common js "fetch.js" to request data from server. when get a special code like -1, I want to redirect to /login. But I don't know how to access $router in fetch.js file.
Thanks.

Most helpful comment

Here is the solution, first add import:

import router from '@/path/to/router.js';

and then just use router.

(router.js is file with code export default new Router({ ...)

All 3 comments

Hello, your issue has been closed because it does not conform to our issue requirements. Please use the Issue Helper to create an issue - thank you!

Here is the solution, first add import:

import router from '@/path/to/router.js';

and then just use router.

(router.js is file with code export default new Router({ ...)

@dima74 Thank you very much.

Was this page helpful?
0 / 5 - 0 ratings