场景
路由跳转的过程相当于是对组件的实例化,销毁过程。
这种机制能够应付绝大部分业务场景,但是在大部分管理系统当中,从 列表 -> 编辑 -> 保存 -> 返回列表 该场景需要保存类似搜索,分页等状态数据,此时则需要使用 Angular 的 RouteReuseStrategy 贯穿路由状态并决定构建组件的方式。
RouteReuseStrategy
shouldDetachDetermines if this route (and its subtree) should be detached to be reused later.storeStores the detached route.shouldAttachDetermines if this route (and its subtree) should be reattached.retrieveRetrieves the previously stored route.shouldReuseRouteDetermines if a route should be reused.
执行顺序如下:
SimpleReuseStrategy
网上很多例子在这里都是用的 route.routeConfig.path 作为 key。
如果不涉及到lazy load是不会有问题的,但是如果存在 loadChildren,那么route.routeConfig.path就有可能会重复。
于是我将 key 改为了 route['_routerState'].url 取的绝对路径。
1 | import { |
注册策略到 forRoot 模块。
lazy laod 的
forChild模块是无法注册的
1 | ({ |
具体模块使用
1 | const routes: Routes = [{ |