【问题标题】:What is proper type for vuex store returned to the vuex plugin during it initialisationvuex 存储在初始化期间返回给 vuex 插件的正确类型是什么
【发布时间】:2020-03-25 11:27:36
【问题描述】:

存储初始化是通过以下方式完成的:

/* Logic */
import Vue from 'vue'
import Vuex, { StoreOptions } from 'vuex'

Vue.use(Vuex)

const DataManager = new UserDataManager()

type RootStateType = {
  version: string
}

const store: StoreOptions<RootStateType> = {
  // @ts-ignore
  state: {
    version: '1.0.0'
  },
  modules: {...},
  plugins: [DataManager.init()]
}

export default new Vuex.Store<RootStateType>(store)

...而插件:

export class UserDataManager {
  ... logic

  public init () {
    return (store: <TYPE?>) => {
      store.watch(
        (state: RootStateType) => {
          return [state.user.authentication.id]
        },
        (watched: string[]) => {
          const userID = watched[0]
          ... logic
        }
      )
    }
  }
}

如果可以在这里传递正确的类型,我有点困惑?我试过传递给它:

type storeType = StoreOptions<RootStateType>

...
    return (store: storeType) => {
...

但它返回: TS2339: Property 'watch' does not exist on type 'StoreOptions&lt;RootStateType&gt;' 如果有人能就这个话题分享一些想法,我将不胜感激:-)

【问题讨论】:

    标签: typescript vuex


    【解决方案1】:

    查看vuex-persistStore&lt;RootStateType&gt; 应该可以工作。

    【讨论】:

    • 确实是这样 :-)
    猜你喜欢
    • 2021-09-16
    • 2018-11-24
    • 2022-11-22
    • 1970-01-01
    • 2021-03-30
    • 2021-03-22
    • 2019-12-21
    • 2018-03-02
    • 2019-12-14
    相关资源
    最近更新 更多