欢迎您开始 @medux 之旅,建议您依次阅读如下 4 篇文章,这将耗费您大约 30 分钟。javascript
第 4 篇:medux 数据流vue
-- Github 地址 ---java
由于 Medux 基于 Redux,因此部分数据流与 Redux 很类似,好比:react
...
await this.dispatch(this.action.searchList());
this.dispatch(this.action.showPop());
复制代码
你只需在定义 Effect 的装饰器中加入可控的参数既可注入其 loading 状态到 moduleState 中:git
// loadingForGroupName 注入加载状态的分组key,默认为global,若是为null表示不注入加载状态
// loadingForModuleName 可将loading状态合并注入到其余module,默认为入口主模块
function effect(loadingForGroupName?: string | null, loadingForModuleName?: string)
// 例如
@effect('global')
public async login(username:string, password:string){
...
}
复制代码
除了 loading 状态,你还能够直接编写 effect 执行先后的钩子:github
function logger(before: (action: Action, moduleName: string, promiseResult: Promise<any>) => void, after: null | ((status: 'Rejected' | 'Resolved', beforeResult: any, effectResult: any) => void));
复制代码
reducer 或 effect 咱们统称为 ActionHandler,当执行 store.dispatch(action)时,会触发一个目标 ActionHandler 的执行,咱们称之为主ActionHandler
。除了主 ActionHandler,还能够存在一系列从ActionHandler
来配合执行,它们能够属于不一样的 module,因此一般用来解决 module 之间的协做。web
从本文顶部的 medux 数据流示意图中看出,蓝色的 Action 彷佛像一条总线穿透各个 module,它的 Event 性质让整个模块变得松散起来typescript
一个 Action 被 dispatch 可能引发一系列 reducer 和 effect 执行,那么它们的执行顺序是怎样的呢?redux
主ActionHandler
老是先执行从ActionHandler
默认是按注册顺序,可是你能够设置 Action.priority 来强制干预interface Action {
type: string;
priority?: string[]; //执行优先级
payload?: any[];
}
复制代码
当 actionHandler 执行出错时,medux 会自动 dispatch 一个 type 为medux.Error
的新 Action,你能够 handler 这个 ErrorAction,并对错误进行处理:api
View 本质上就是一个 Component,可是 View 用来展现业务,Component 用来展现交互。从本文最开始的 Medux 数据流示意图中看出:
const RoleSelector = loadView('adminRole', 'Selector');
复制代码
框架会自动监听路由的变化,并将路由信息解析为 RouteState,而后:
dispatch medux.RouteChange
Action 将其注入 Redux 一级子节点 route 中dispatch moduleName.RouteParams
Action 将其注入相应的 moduleState喜欢 vue 或 mobx 的朋友可能会问,medux 是要求可变数据仍是不可变数据?
虽然 medux 是基于 redux 的,但本着实用至上的原则,并不要求严格遵循 redux 模型,它是另外一个 flux 框架。
medux 框架内部会使用 ImmutableData 来自动生成并管理 state 及其 1 级节点,对于这个内置数据结构一般你也无需干预。而对于次级的 moduleState 你能够将它定义为 一个 MutableData,而后直接在 reducer 中修改 state 并返回它,尽管这有违 reducer 的本意,但这是对接 MutableData 最简单灵活的方案。
@medux/react-web-router:整合封装了@medux/core、@medux/web、@medux/route-plan-a、@medux/react, 是 web 环境下开发 react 的开箱即用框架
medux-react-admin:基于@medux/react-web-router
和最新的ANTD 4.x
开发的通用后台管理系统,除了演示 medux 怎么使用,它还创造了很多独特的理念