Vuex-module-decorators: store state Object is of type unknown

Created on 13 Aug 2019  路  6Comments  路  Source: championswimmer/vuex-module-decorators

/src/middlewares/auth.ts file:

import store from '@/store'

export default {
  guest(): void {
    if (store.state.auth.authenticated === false) {
      // do some action
    }
  }
}

/src/store.ts file:

import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  modules: {
    auth
  }
})

/src/store/auth.ts:

import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators'

@Module
export default class Auth extends VuexModule {
  public authenticated: boolean = false
}

However i'm getting TS error: Object is of type 'unknown'

Then my npm run build fails. How can i type store.state.auth.authenticated so this error would disappear?

image

All 6 comments

getModule is how you access the typed store.

Thanks, is there an example?

https://championswimmer.in/vuex-module-decorators/pages/getting-started.html#access-state

However, just make sure that the store and module exist with Vue devtools before trying to implement getModule.

NB: Looking at how you've defined your modules, you'll need to pass the store to getModule as a second parameter.

Both store and module exist in devtools, you mean getModule(myModule, store)? This doesn't work, throws an error.

Tried now:

/src/store/auth.ts:

import { Module, VuexModule, Mutation, Action, getModule } from 'vuex-module-decorators'
import store from '@/store'

@Module({
  name: 'auth', 
  store
})
class Auth extends VuexModule {
  public authenticated: boolean = false
}

export default getModule(Auth)

And /src/middlewares/auth.ts:

import auth from '@/store/auth'

export default {
  guest(): void {
    if (auth.authenticated === false) {...}
  }
}

Error is gone, but my app is not working at all now, getting an error:

TypeError: Cannot read property 'getters' of undefined

If i completely omit modules thing just like in this example: https://github.com/xieguangcai/vue-order-admin, then i'm getting error about unknown actions... So much pain.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TotomInc picture TotomInc  路  7Comments

erathorus picture erathorus  路  5Comments

blainehansen picture blainehansen  路  4Comments

davidmoshal picture davidmoshal  路  6Comments

Akumzy picture Akumzy  路  5Comments