【问题标题】:Vuex - store state Object is of type unknownVuex - 存储状态对象的类型未知
【发布时间】:2019-12-19 09:51:36
【问题描述】:

/src/middlewares/auth.ts 文件:

import store from '@/store'

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

/src/store.ts 文件:

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
}

但是我收到 TS 错误:Object is of type 'unknown'

然后我的npm run build 失败了。我怎样才能输入store 这样这个错误就会消失?

更新 1

问题出在:if (store.state.auth.authenticated === false)

更新 2

这三个文件的目录结构:

  • /src/middlewares/auth.ts
  • /src/store.ts
  • /src/store/auth.ts

更新 3

如果我将 store.state.auth.authenticated 更改为 store.state 然后编译器停止抱怨,我认为它与我的商店 auth.ts 文件有关,我需要以某种方式输入定义。

【问题讨论】:

  • 您能否提供更多上下文并指出错误所在的行?没有足够的代码来重现错误。
  • @tony19 更新了我的问题。
  • 你的目录/文件结构是什么样的,至少对于这两个文件来说是这样的?
  • @Phil,我正在使用 vuex-module-decorators。

标签: javascript typescript vue.js vuex


【解决方案1】:

看一看:logrocket

export default function auth ({ next, store }){
 if(!store.getters.auth.loggedIn){
     return next({
        name: 'login'
     })
 }

 return next()
}

您的第一个参数应该是定义 { store } 的对象

export default guest ({ store }, to: any ...){

【讨论】:

    猜你喜欢
    • 2020-06-21
    • 2018-08-17
    • 2020-10-22
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 2019-03-21
    • 1970-01-01
    • 2019-10-28
    相关资源
    最近更新 更多