championswimmer / vuex-module-decorators

TypeScript/ES7 Decorators to create Vuex modules declaratively
https://championswimmer.in/vuex-module-decorators/
MIT License
1.8k stars 170 forks source link

Do I have to have NuxtJS to use getModule? #280

Open rostamiani opened 4 years ago

rostamiani commented 4 years ago

How can I access the getModule without NuxtJS? I'm using Vuejs. How can I access store states and actions with types? Do I have to have NuxtJS to use getModule?

Why getModule just used in the NustJS examples? Is it a good idea to export getModule(store) in the store/index.ts? Why this isn't done in the examples?

like this:

const store = new Vuex.Store({
  state: {},
  modules: {
    counter,
    hgapi
  }
})

export getModule(store);
rostamiani commented 4 years ago

I tried to use it like this, but have errors:

import {
  Module,
  VuexModule,
  Mutation,
  Action,
  getModule,
} from 'vuex-module-decorators'
import Vuex from 'vuex'
import Vue from 'vue'

export interface RootState {
  count: number
}

@Module
class TestModule extends VuexModule {
  count = 0

  @Mutation
  increment(delta: number) {
    this.count += delta
  }

  // action 'incr' commits mutation 'increment' when done with return value as payload
  @Action({ commit: 'increment' })
  incr() {
    return 5
  }
}
// Error:
// Argument of type 'TestModule' is not assignable to parameter of type 'ConstructorOf<VuexModule<ThisType<any>, any>>'.
const testModule = getModule(TestModule.prototype)

Vue.use(Vuex)

export const store = new Vuex.Store<RootState>({
  modules: {
    testModule,
  },
})

// Error:
// Argument of type 'Store<RootState>' is not assignable to parameter of type 'ConstructorOf<VuexModule<ThisType<any>, any>>'.
const TStore = getModule(store)