Vuex Typescript 支持

1. 定义

1.1 定义入口模块

入口 /src/store/index.ts :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import type {InjectionKey} from 'vue';
import {createStore, Store, useStore as baseUseStore} from 'vuex';
// 引入子模块
import createUserState, {IUserState} from './user';

export interface IRootState {}

export interface IModuleState {
user: IUserState;
// other modules
}

export const storeInjectionKey: InjectionKey<Store<IRootState & IModuleState>> = Symbol('storeInjectionKey');

export default function initStore() {
return createStore<IRootState>({
state: {},
mutations: {},
actions: {},
modules: {
user: createUserState(),
// other modules
},
});
}

// 定义自己的 `useStore` 组合式函数
export function useStore() {
return baseUseStore(storeInjectionKey);
}

1.2 定义子模块

User 模块 /src/store/user.ts :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import type {InjectionKey} from 'vue';
import {Store} from 'vuex';
import type {Module} from 'vuex';
import {IRootState} from '.';
import {getUserDetail, getUserStatus} from '@/base/api/user';
import {IS_NODE} from '@/base/utils';

export interface IUserDetail {
id?: number;
ucid?: string;
unfinished_orders_num?: number;
orders_num?: number;
has_saas?: boolean;
}

// 定义 UserState 的接口
export interface IUserState {
isLogin: boolean;
ucid?: string;
ucname?: string;
loginType?: 'uc';
detail?: IUserDetail;
}

// 可选
export const storeUserKey: InjectionKey<Store<IUserState>> = Symbol('storeUserKey');

// 导出一个创建 State Module 的方法,防止在 SSR 场景下每个请求都使用同一个实例
function createUserState(): Module<IUserState, IRootState> {
namespaced: true,
state: () => ({
isLogin: false,
}),
mutations: {
setLoginStatus(state, payload) {
state.isLogin = payload.isLogin;
state.loginType = payload.loginType;
},
setLoginInfo(state, payload) {
state.ucid = payload.ucid;
state.ucname = payload.ucname;
},
setUserDetail(state, payload) {
state.detail = payload;
},
setLoginOut(state) {
state.isLogin = false;
},
},
actions: {
getUserStatus: async (store, payload) => {
// do somethings
},
getUserDetail: async (store, payload) => {
// do somethings
},
},
getters: {},
};

export default createUserState

2. 使用

1
2
3
4
5
6
7
import {computed} from 'vue'
import {useStore} from '/src/store';

const {state, dispatch} = useStore();

const ucid = computed(state => state.user?.ucid)
dispatch('user/getUserStatus');