Skip to content
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

feat: preserve state with registerModule #837

Merged
merged 1 commit into from
Oct 11, 2017
Merged

Conversation

blake-newman
Copy link
Member

@blake-newman blake-newman commented Jun 22, 2017

As speaking with @TheLarkInn we where saying it would be awesome to be able to register a module dynamically to be able to code split the store in a large application.

It is totally possible with store.registerModule('a', module). Here is an example, if using hooks to ensure prevent the route loading. This can be achieved with asyncData with client mixins and global hooks. https://ssr.vuejs.org/en/data.html

export default {
  async asyncData ({ registerModule, dispatch }) {
    registerModule('a', await import('@/store/modules/a'))
    dispatch('a/action')
  }
}

The issue with this with SSR this would replace the pre-populated data. This pull request aims to fix this scenario.

Copy link
Member

@ktsn ktsn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@ktsn
Copy link
Member

ktsn commented Jun 22, 2017

This would fix #789

@blake-newman
Copy link
Member Author

Awesome, had this in my mind for months just forgot about it.

@TheLarkInn
Copy link

TheLarkInn commented Jun 22, 2017

does this mean you could pass the import directly also?

this.$store.replaceModule('a', import('@/store/modules/a'))

or similar to Vue.use that it is a fn that returns the Promise<module>?

this.$store.replaceModule('a', () => import('@/store/modules/a'))

@blake-newman
Copy link
Member Author

Not yet! This is my next thing to do. Small things at a time haha!

@TheLarkInn
Copy link

😎 np cheers on this addition for SSR

@andreiglingeanu
Copy link

🤘

@blake-newman
Copy link
Member Author

@TheLarkInn I'm going to go with the latter, as it will require less code in Vuex and a simple integration. Also doesn't require the need to do custom waiting mechanisms for promise to resolve.

@TheLarkInn
Copy link

So like this?

this.$store.replaceModule('a', () => import('@/store/modules/a'))

@blake-newman
Copy link
Member Author

yes but it is registerModule

@blake-newman
Copy link
Member Author

/ping @vuejs/collaborators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants