awex
针对小程序的状态管理工具
特性
- 支持typescript
- 使用简单
- 支持模块
- 支持异步action
example
初始化
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<void>;
}
export default Store.createStore<IState, IStoreActions>({
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;
},
});
使用
import store from './store';@Provider(store)class MyApp implements AppOptions {onLaunch() {store.actions.getSystemInfo();}}App(new MyApp());
interface IData extends IHomeState {} @store.connect(state => ({ ...state.home!,}))class PageOptions { pageWillReceiveData(data: IData) { // console.log('change=>', data); } update = (data: Partial<IData>) => { store.actions.home!.updateState(data); };}