@@ -11,6 +11,7 @@ type CommitNameType = AppStateType & ConsoleStateType & UserStateType
1111
1212/**
1313 * @description setStoreState -方法是一个 mutaitions 的操作
14+ * @type {T } T - 你要更改的模块的类型
1415 * @param {string } module - 要操作的state 的 module 名
1516 * @param {string } key - 要操作的state 的 module 下的 key 值
1617 * @param {any } value - 当有 msg 参数时,视为赋值操作,触发 mutation,msg 则为要复制的数据.
@@ -24,17 +25,39 @@ type CommitNameType = AppStateType & ConsoleStateType & UserStateType
2425 * }
2526 * }
2627 * ```
27- * 想要单独修改 firstName,直接使用 setStoreState('app','name',{firstName:'modifiedName',lastName:'Ma'})
28+ * 想要单独修改 firstName,直接使用 setStoreState<AppStateType> ('app','name',{firstName:'modifiedName',lastName:'Ma'})
2829 */
2930
30- export const setStoreState = (
31+ export function setStoreState < T > (
3132 module : ModuleNameType ,
32- key : keyof CommitNameType ,
33+ key : keyof T ,
3334 value : any
34- ) => {
35+ ) {
3536 store . commit ( {
3637 type : module + '/__set' ,
3738 key : key ,
3839 val : value
3940 } )
4041}
42+
43+ /**
44+ * @description 封装 dispatch 方法
45+ * @type {T } T 你要派发actions的模块的类型
46+ * @example 使用方法如下 const result = await dispatchActions<UserActionsType>('console','refreshToken',1)
47+ */
48+ export function dispatchAction < T > (
49+ module : ModuleNameType ,
50+ key : keyof T ,
51+ value ?: any
52+ ) {
53+ store . dispatch ( `${ module } /${ key } ` , value )
54+ }
55+
56+ /**
57+ * @description 封装 dispatch 方法
58+ * @type {T } T 你要获取 getters的模块的类型
59+ * @example 使用方法如下 const result = getStoreGetter<ConsoleGetterType>('console','list')
60+ */
61+ export function getStoreGetter < T > ( module : ModuleNameType , key : keyof T ) {
62+ store . getters [ `${ module } /${ key } ` ]
63+ }
0 commit comments