store miniprogram microapp typescript
npm install awex针对小程序的状态管理工具
* 支持typescript
* 使用简单
* 支持模块
* 支持异步action
##### 初始化
``
import Store, { IOptions, IActions } from 'awex';
import apis, { ISystemInfo } from 'mp-apis';
import homeModel, { IHomeState, IHomeActions } from '../pages/home/store';
export interface IState {
home?: IHomeState;
systemInfo: ISystemInfo;
}
export interface IStoreActions extends IActions {
home?: IHomeActions;
getSystemInfo(): Promise
}
export default Store.createStore
log: true,
modules: {
home: homeModel,
},
state: {
systemInfo: {} as ISystemInfo,
},
actions: {
getSystemInfo: async ({ state }) => {
const system = await apis.getSystemInfo();
state.systemInfo = system;
},
},
setStorage: state => {
const app = getApp();
app.store = state;
},
});
``
##### 使用 app.ts
import store from './store';
@Provider(store)
class MyApp implements AppOptions {
onLaunch() {
store.actions.getSystemInfo();
}
}
App(new MyApp());
`
`home page
interface IData extends IHomeState {}
@store.connect(state => ({
...state.home!,
}))
class PageOptions {
pageWillReceiveData(data: IData) {
// console.log('change=>', data);
}
update = (data: Partial
store.actions.home!.updateState(data);
};
}
``