I want to do this.$router.push(path)
inside module file of vuex.
As this is not defined there, how could I perform this.
Asked
Active
Viewed 1,185 times
3

skns
- 132
- 1
- 8
-
[Check this](https://stackoverflow.com/a/42604186/2815635) – Niklesh Raut Sep 28 '20 at 07:38
-
@NikleshRaut seems like the OP want to it the opposite way, accessing the router in Vuex and not the state in routes. – Yair Cohen Sep 28 '20 at 08:37
1 Answers
3
Just import the router and you'll be able to use it, like this:
import router from 'path/to/router'
router.push(path)
Why does it work like this:
In a Vue file, this
is binded to the Vue object which is why you can use certain methods that are available there like $router or $store.
However in a normal JS file this
is just binded to the global object that does not contain any of Vue's special functionality, which is why you have to import the router manually.

Yair Cohen
- 2,032
- 7
- 13