michaelolof / vuex-class-component

A Type Safe Vuex Module or Store Using ES6 Classes and ES7 Decorators written in TypeScript.
217 stars 21 forks source link

Namespaced modules still broken #52

Closed SaphuA closed 5 years ago

SaphuA commented 5 years ago

Okay so I was a little too optimistic when I said your last commit fixed the namespace issues. It seems that only the namespace is used of the last store that is touched or used and therefore only this store is working (which was the only page I was testing at the time...).

I'll look into it and update this issue accordingly.

SaphuA commented 5 years ago

It seems like the options are used of the last extracted module, or something like that...

export class StoreA extends createModule({ namespaced: "StoreA", strict: true }) { }
export class StoreB extends createModule({ namespaced: "StoreB", strict: true }) { }
console.log(extractVuexModule(StoreA));
console.log(extractVuexModule(StoreB));
export class StoreC extends createModule({ namespaced: "StoreC", strict: true }) { }
console.log(extractVuexModule(StoreA));
console.log(extractVuexModule(StoreB));
console.log(extractVuexModule(StoreC));

prints

{StoreB: {…}}
{StoreB: {…}}
{StoreB: {…}}
{StoreB: {…}}
{StoreC: {…}}
SaphuA commented 5 years ago

Seems very likely this has something to do because you're using the prototype here, which is always VuexModule:

https://github.com/michaelolof/vuex-class-component/blob/a374852288de1f5454755123215c5f115bfb9a4f/src/module.ts#L18

michaelolof commented 5 years ago

Mind sharing a sample project so I can replicate your issue.

Thanks

SaphuA commented 5 years ago

Here you go. I've added some console.log's to src/store.ts

yarn install
yarn serve

https://github.com/SaphuA/vuex-class-component-ns-repro

michaelolof commented 5 years ago

This issue has been resolved in the latest version

SaphuA commented 5 years ago

Looks ok! I'll do some more testing this week.

SaphuA commented 5 years ago

So when exactly should one use createModule and when With for providing options?

michaelolof commented 5 years ago

With is useful for extending already defined options.

Ideally you want to create a base configuration like so:

const VuexModule = createModule({
  strict: false,
  target: "nuxt",
  enableLocalWatchers: true,
})

export class UserStore extends VuexModule.With({ namespaced: "user", strict: true }) {
}

Think of it as a 2 level inheritance for your module options.