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

Create store in component? #16

Closed
Kukks opened this issue Sep 13, 2018 · 7 comments
Closed

Create store in component? #16

Kukks opened this issue Sep 13, 2018 · 7 comments

Comments

@Kukks
Copy link

Kukks commented Sep 13, 2018

How can I create stores at runtime on demand? I have some pages that need to have their own specific namespaced modules

@championswimmer
Copy link
Owner

// @/store/index.ts
import {Store} from 'vuex'
export default store = new Vuex.Store({})
// @/components/mycomponent.ts (or .vue <script> portion)
import {Component, Vue} from 'vue-property-decorator'
import store from '@/store/index.ts'
import {VuexModule} from 'vuex-module-decorators'

@Module({dynamic: true, namespaced: true, name: 'mymodule', store})
class MyModule extends VuexModule {
  somestuff = 'awesome stuff'
  @Mutation
  doSomething() {}
}
const mm = getModule(MyModule)

@Component
export default class MyComponent extends Vue {
   somedata = mm.somestuff
   somemethod() { mm.doSomething() }
}

@Kukks
Copy link
Author

Kukks commented Sep 13, 2018 via email

@championswimmer
Copy link
Owner

championswimmer commented Sep 13, 2018

You mean do the store.registerModule('mm', myModule) part on your own ?

@Kukks
Copy link
Author

Kukks commented Sep 14, 2018

Yes @championswimmer

@Kukks
Copy link
Author

Kukks commented Sep 14, 2018

Would I be able to do store.registerModule('mm', MyModule) and not mark it as dynamic?

@championswimmer
Copy link
Owner

If you do not mark is dynamic then you can import the module class and register it into the store.

// @/store/moduleA.ts

@Module
export default class ModuleA extends VuexModule {
}
// @/components/Comp.vue
import ModuleA from '@/store/moduleA'

this.$store.registerModule('name', ModuleA)

@jordisan
Copy link

I this case, how do you access the module from the component? Can you use getModule() and benefiting from the typing, intellisense, etc.?

If you do not mark is dynamic then you can import the module class and register it into the store.

// @/store/moduleA.ts

@Module
export default class ModuleA extends VuexModule {
}
// @/components/Comp.vue
import ModuleA from '@/store/moduleA'

this.$store.registerModule('name', ModuleA)

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

No branches or pull requests

3 participants