Ale*_*Kim 9 javascript typescript vue.js vuex
/src/middlewares/auth.ts 文件:
import store from '@/store'
export default {
guest(): void {
if (store.state.auth.authenticated === false) {
// do some action
}
}
}
Run Code Online (Sandbox Code Playgroud)
/src/store.ts 文件:
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export default new Vuex.Store({
modules: {
auth
}
})
Run Code Online (Sandbox Code Playgroud)
/src/store/auth.ts:
import { Module, VuexModule, Mutation, Action } from 'vuex-module-decorators'
@Module
export default class Auth extends VuexModule {
public authenticated: boolean = false
}
Run Code Online (Sandbox Code Playgroud)
但是我收到 TS 错误: Object is of type 'unknown'
然后我的npm run build失败了。我如何输入才能store使此错误消失?
更新 1
问题是: if (store.state.auth.authenticated === false)
更新 2
我这三个文件的目录结构:
更新 3
如果我store.state.auth.authenticated改为store.state然后编译器停止抱怨,我认为它与我的商店 auth.ts 文件有关,我需要以某种方式对其进行类型定义。
小智 1
看看:logrocket
export default function auth ({ next, store }){
if(!store.getters.auth.loggedIn){
return next({
name: 'login'
})
}
return next()
}
Run Code Online (Sandbox Code Playgroud)
你的第一个参数应该是一个定义 { store } 的对象
export default guest ({ store }, to: any ...){
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1804 次 |
| 最近记录: |