Getting router params into Vuex actions

后端 未结 3 492
眼角桃花
眼角桃花 2021-02-07 12:08

I would like to pass router params into Vuex actions, without having to fetch them for every single action in a large form like so:

edit_sport_type({ rootState,          


        
3条回答
  •  南方客
    南方客 (楼主)
    2021-02-07 12:48

    To get params from vuex store action, import your vue-router's instance, then access params of the router instance from your vuex store via the router.currentRoute object.

    Sample implementation below:

    router at src/router/index.js:

    import Vue from 'vue'
    import VueRouter from 'vue-router'
    import routes from './routes'
    
    Vue.use(VueRouter)
    
    const router = new VueRouter({
      mode: 'history',
      routes
    })
    
    export default router
    

    import the router at vuex store:

    import router from '@/router'

    then access params at vuex action function, in this case "id", like below:

    router.currentRoute.params.id
    

提交回复
热议问题