主题
实例与事件
js
const instance = await modal.openInstance('demo');
if (await instance.ready) {
const off = instance.subscribeState(
state => state.loading.visible,
visible => console.log(visible),
Object.is,
['loading']
);
const offRoute = instance.on('routeChange', event => console.log(event.to));
await instance.result;
off();
offRoute();
}ready 表示导航命令注册,不表示动画或懒加载完成。getSnapshot 返回六个 Store 的聚合快照,关闭后为 null。subscribeState 不初始通知,依赖必须覆盖 Selector 读取的全部 Store;省略则订阅全部。快照只读,跨 Store 更新非原子。
实例事件:ready、change、destroy、routeChange、routeRemove、routeBlur、routeFocus。路由事件基于已发布快照差异,不是每次命令或 React 挂载事件;参数变化不触发路由切换事件。Manager 事件:open、close、queueChange。事件不重放,避免在监听器内重入导航。
replaceOptions 替换覆盖配置;dispose 强制销毁并绕过守卫。旧实例不能操作新实例。
实例命令速查
| 方法 | 用途 |
|---|---|
| ready / result | 等待命令就绪 / 业务结束 |
| isActive / id | 判断实例有效性 / ID |
| getState / subscribe | 导航状态及订阅 |
| getSnapshot / subscribeState | 聚合状态及选择器订阅 |
| navigate / clearCache | 导航 / 清除后台缓存 |
| setParams / setOptions | 更新参数 / 布局覆盖 |
| replaceOptions | 替换覆盖配置 |
| setLoading / setFullscreen | Loading / 全屏 |
| close / confirm | 关闭 / 明确确认 |
| dispose | 强制释放,仅用于所有者销毁等场景 |
| on / off | 实例事件订阅 |
js
const off = modal.on('close', ({ name, result }) => console.log(name, result.status));
// 组件卸载时调用 off()