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; }
export const storeInjectionKey: InjectionKey<Store<IRootState & IModuleState>> = Symbol('storeInjectionKey');
export default function initStore() { return createStore<IRootState>({ state: {}, mutations: {}, actions: {}, modules: { user: createUserState(), }, }); }
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; }
export interface IUserState { isLogin: boolean; ucid?: string; ucname?: string; loginType?: 'uc'; detail?: IUserDetail; }
export const storeUserKey: InjectionKey<Store<IUserState>> = Symbol('storeUserKey');
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) => { }, getUserDetail: async (store, payload) => { }, }, 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');
|