-
Notifications
You must be signed in to change notification settings - Fork 238
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
registerModule
would be better to called in beforeCreate
instead of asyncData
#166
Comments
Also |
So how to preserve store module be registered on client? I can only register the module again before dispatching an action on client now, is this as expected? |
Ah, that's right. So for now calling |
I probably need a bit more time to properly rethink this, but if you have a reasonable workaround feel free to submit a PR for the moment. |
@yyx990803 I found more problems here. My project store structure is not much like recommended way as document. Store will be created for every user request with a single http request (axios exactly) with user headers for authorization, so store actions should request with the singleton axios, like Example: Pass axios into every action as part of payload is really boring, and it will be great if we can add some custom global property on It works very well before I starting using When I try to And also, After the first render on client, if we leave this route then My workaround for now is checking whether the module has been registered on client, example: https://github.com/JounQin/Rubick/blob/master/src/views/container/application/Application.vue#L38. hasModule may help with this or a new option for |
just ran up against this and spent a couple hours trying to figure out why the docs were saying what they were saying.. Anyway an idea:
|
I just ran into the same problem, any news on this? I think I almost had a solution with |
I ended up writing my own handler for store modules like the approach I suggested |
Could you please create an exmple repo of your solution? I'm new to vue and i don't really understand what do you mean by "add a utility to the Vue prototype to allow adding store modules the same way as vue components. " |
@jumika just an example router.onReady(() => {
const loadRouteComponents = router.getMatchedComponents()
loadRouteComponents.forEach(c => {
if (c.storeModule) {
c.storeModule.forEach(mod => {
if (!store._modules.root._children[mod.name]) {
store.registerModule(mod.name, mod)
}
})
}
})
app.$mount("#app")
}) |
I register store in server side,only state can use in client side, mapGetter and mapActions not working.how to resolve it. |
Yeah, Im having the same issue and I can't find a proper workaround for this. Any idea guys ? And btw, wouldn't be great to have the source code of the full project ? Something we can use as reference during our local experiments ;) |
@devCrossNet Thanks ! This definitely put me on the right track !! What I ended up doing, as suggested on your reference (and so this solutions is also here in case someone else is coming, or in case someone have some toughs on it), is to create a helper function in charge of load modules: export function registerStoreModule({ module, moduleName, store }) {
const moduleIsRegistered = store._modules.root._children[moduleName] !== undefined;
const stateExists = store.state[moduleName];
if (!moduleIsRegistered) {
store.registerModule(moduleName, module, { preserveState: stateExists });
}
} This function will only register the module if it wasn't registered before. Then, on your modules (where you wanna lazy load your nested store); you declare two methods:
...
asyncData ({ store }) {
registerStoreModule({ module: homeStore, moduleName: storeName, store });
return store.dispatch('home/add');
},
...
beforeCreate() {
console.log('before');
registerStoreModule({ module: homeStore, moduleName: storeName, store: this.$store });
},
... |
@yyx990803 When we are using SSR, client will not call
asyncData
at first render, then we will not be able to use registered store module because it has not been registered on client.So, if it is as expected,
registerModule
would be better to called inbeforeCreate
instead ofasyncData
, so that it will be registered on client at first render, of coursepreserveState: process.env.VUE_ENV !== 'server'
should also be used.The text was updated successfully, but these errors were encountered: