javascript - Vuex - 存储状态对象的类型未知
问题描述
/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
我对这 3 个文件的目录结构:
- /src/middlewares/auth.ts
- /src/store.ts
- /src/store/auth.ts
更新 3
如果我store.state.auth.authenticated
改为store.state
然后编译器停止抱怨,我认为它与我的商店 auth.ts 文件有关,我需要以某种方式输入定义。
解决方案
看一看: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 ...){
推荐阅读
- dataframe - 在火花数据框中的空值列上应用聚合时出错
- seq2seq - 训练 seq2seq 模型时出现 InvalidArgumentError
- c# - 如果代码中的某些条件不匹配,而不是通过验证 Sql 查询,如何停止 SAP B1 Form_Data_Add 事件
- jquery - 移动设备上的导航菜单下拉菜单在点击后不会消失
- c++ - 如何将私有数据添加到 redisAsyncContext 以便在连接回调中使用我的数据?
- sql-server - 从同一网络域 PC 从 Access 2013 连接到 SQL Server 2017 时,多步 OLE DB 操作生成错误
- database - 我可以在浏览器中创建数据库吗?除了 localstorage/indexedDB/session 存储
- wordpress - 如何使用 getChildHtml() 从 Magento 获取块并将其插入 Wordpress 标题?
- reactjs - 有没有办法从父组件访问formik的提交函数、值和其他道具
- sql-server - Hibernate 一对多准备好的语句非常慢