你應該實現自定義RouteReuseStrategy
創建服務實施RouteReuseStrategy
:
export class CustomReuseStrategy implements RouteReuseStrategy {
handlers: {[key: string]: DetachedRouteHandle} = {};
shouldDetach(route: ActivatedRouteSnapshot): boolean {
return true;
}
store(route: ActivatedRouteSnapshot, handle: DetachedRouteHandle): void {
this.handlers[route.routeConfig.path] = handle;
}
shouldAttach(route: ActivatedRouteSnapshot): boolean {
return !!route.routeConfig && !!this.handlers[route.routeConfig.path];
}
retrieve(route: ActivatedRouteSnapshot): DetachedRouteHandle {
if (!route.routeConfig) return null;
return this.handlers[route.routeConfig.path];
}
shouldReuseRoute(future: ActivatedRouteSnapshot, curr: ActivatedRouteSnapshot): boolean {
return future.routeConfig === curr.routeConfig;
}
}
並添加此供應商到@NgModule
:
providers: [
{provide: RouteReuseStrategy, useClass: CustomReuseStrategy}
]
您可以調整CustomReuseStrategy
只有某些路徑可以重用組件,但我會留給你o找出如何。
source
的【如何重定向到從angular2路線外部URL沒有成分?]可能的複製(https://stackoverflow.com/questions/40150393/how-to-redirect-to-an-外部url-from-angular2-route-without-using-component) – k11k2