-
Notifications
You must be signed in to change notification settings - Fork 125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create actions to expose router methods #68
Comments
Did you know you can import the router and use it: // actions.js
import router from './router'
export function anAction (context) {
router.push('/profile/1')
} |
So I wanted to do some extra processing on some query params so i wanted to leverage getters to provide the parsed data. Below: While i_can_do_this, I think I_prefer_this
And it works! The sync is probably taking care to merge the route module and the existing route module. So I also tested this with actions and it's totally fine too.
|
I finally found a good solution to use router on actions:
The constant strictly needs to be exported to use it later on actions.js:
|
you can also do
before creating the store but i have no idea this is good or bad |
There is a use case where simple importing UPDATE // store/index.js
export const createStore = (router) => {
return new Vuex.Store({
state: {},
mutations: {},
actions: {},
getters: {
router: () => router
},
modules: {...storeModules}
})
} And just use the // store/my-module/actions.js
export const updateOrder = ({rootGetters, getters, dispatch}, order) => {
const query = getters.params
query['order'] = order
rootGetters.router.push({query})
dispatch('fetchPosts')
} // store/my-module/getters.js
export const params = (state, getters, rootState) => {
const params = {}
params['page'] = state.pagination.page
params['order'] = rootState.route.query.order
return params
} |
@cannap's solution seems to work quite well - I think it's the superior choice because it is significantly more generalized, and you don't have to add any extra code. |
@outkine the problem with the @cannap solution is that you are changing the Vuex.Store node module prototype (its a kind of static variable), so if you need to instantiate your store twice or more you'll get the same router on every vuex store instances. Since VueSSR is a long running process each request should have their own instance of Vue, Vuex, VueRouter, and so on. Making that solution not viable. |
That definitely makes sense, and is probably the best long-term solution. In my case I was just looking for a quick patch, as I don't have SSR. |
Is there any update on this? I can work on this issue/feature if the maintainers agree to, I can not waste time on something useless that will be discarded and not merged. |
Can we consider adding some vuex actions for the following methods:
Right now, it's a little awkward in that we can access the state from Vuex using this plugin, but you are still required to interact with the router via direct access.
With this proposal, I want the flow to look something akin to this (which I believe is how integrations between react-router and redux generally function):
The text was updated successfully, but these errors were encountered: