Seu*_*won 16 routes lazy-loading reusability angular
我尝试在我的angular2项目中使用自定义重用策略,但我发现它不适用于延迟模块加载.谁知道这个?我的项目是角度2.6.4
重用strategy.ts
import {RouteReuseStrategy, ActivatedRouteSnapshot, DetachedRouteHandle} from "@angular/router";
export class CustomReuseStrategy implements RouteReuseStrategy {
handlers: {[key: string]: DetachedRouteHandle} = {};
shouldDetach(route: ActivatedRouteSnapshot): boolean {
console.debug('CustomReuseStrategy:shouldDetach', route);
return true;
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
console.debug('CustomReuseStrategy:store', route, handle);
this.handlers[route.routeConfig.path] = handle;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
console.debug('CustomReuseStrategy:shouldAttach', route);
return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
console.debug('CustomReuseStrategy:retrieve', route);
if (!route.routeConfig) return null;
return this.handlers[route.routeConfig.path];
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
console.debug('CustomReuseStrategy:shouldReuseRoute', future, curr);
return future.routeConfig === curr.routeConfig;
}
}
Run Code Online (Sandbox Code Playgroud)
app.module.ts
const appRoutes: Routes = [
{ path: 'crisis-center', component: CrisisListComponent },
{ path: 'heroes', loadChildren: 'app/hero-list.module#HeroListModule' },
{ path: '', redirectTo: '/crisis-center', pathMatch: 'full' }
];
@NgModule({
imports: [ ... ],
declarations: [ ... ],
providers:[
{provide: RouteReuseStrategy, useClass: CustomReuseStrategy}
],
bootstrap: [ AppComponent ]
})
export class AppModule { }
Run Code Online (Sandbox Code Playgroud)
我把<input>它放到了两个组件上,然后我测试了它.
输入的值CrisisListComponent存储但是,HeroListComponent lazy-loaded不保留值.
我不知道它还没有得到支持.感谢你们对我的帮助.
Gau*_*uss 11
RouteReuseStrategy 可以使用LazyLoaded组件.
这里的问题是你route.routeConfig.path用作存储和检索Handles的密钥.
我不知道为什么,但是使用LazyLoaded模块,route.routeConfig.path在执行时是空的shouldAttach
我使用的解决方案是在我的路线中定义自定义键,例如:
{ path: '...', loadChildren: '...module#...Module', data: { key: 'custom_key' } }
Run Code Online (Sandbox Code Playgroud)
可以在以下位置轻松访问此键值ActivatedRouteSnapshot:
route.data.key
Run Code Online (Sandbox Code Playgroud)
使用此键,您可以正确存储和检索手柄.
小智 5
用这个。它使用组件名称作为键来存储和检索句柄。
import {ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy} from '@angular/router';
export class CustomReuseStrategy implements RouteReuseStrategy {
handlers: { [key: string]: DetachedRouteHandle } = {};
shouldDetach(route: ActivatedRouteSnapshot): boolean {
return true;
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
this.handlers[this.getKey(route)] = handle;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
return !!route.routeConfig && !!this.handlers[this.getKey(route)];
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
if (!route.routeConfig) {
return null;
}
return this.handlers[this.getKey(route)];
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return curr.routeConfig === future.routeConfig;
}
private getKey(route: ActivatedRouteSnapshot) {
let key: string;
if (route.component) {
key = route.component['name'];
} else {
key = route.firstChild.component['name'];
}
return key;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4282 次 |
| 最近记录: |