How to get Vue Router Params in Vuex?

江枫思渺然 提交于 2020-12-29 06:20:06

问题


I'm not able to use

this.$route.params
route.params
router.params

how do I get the params of $route in my actions?


回答1:


in your actions.js or store.js

import router from 'path/to/router'
console.log(router.currentRoute.params)



回答2:


You can access the current route through the state or rootState with Vuex.

If the action is in a module, use rootState:

MY_ACTION: ({rootState}) => new Promise((resolve, reject) => {
    let params = rootState.route.params
    // do stuff
}

If the action is not in a module, use state:

MY_ACTION: ({state}) => new Promise((resolve, reject) => {
    let params = state.route.params
    // do stuff
}

See https://vuex.vuejs.org/api/#actions for more information on the context object.



来源:https://stackoverflow.com/questions/50824234/how-to-get-vue-router-params-in-vuex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!