简体   繁体   English

Angular 组件在导航时重新加载

[英]Angular component reloads when navigating

Routes for my Module:我的模块的路线:

 const routes: Routes = [ { path: ":level1/:level2/:level3", component: CategoriesComponent }, { path: ":level1/:level2", component: CategoriesComponent}, { path: ":level1", component: CategoriesComponent}, { path: "", component: CategoriesComponent }, ];

the Categories component Generates some links like so:类别组件生成一些链接,如下所示:

 <a [routerLink]="['category1']" [relativeTo]="activatedRoute">My Link</a>

the ngOnInit & ngOnDestroy are called each time it navigates between those routes.每次在这些路由之间导航时都会调用ngOnInitngOnDestroy

What I need is the component to stay mounted and not re-init each time.我需要的是保持安装状态而不是每次都重新初始化的组件。

Stackblitz link to illustrate the difference between using QueryParameters and RouteParameters: Angular example Stackblitz 链接来说明使用 QueryParameters 和 RouteParameters 之间的区别: Angular 示例

NOTE: Dont suggest RouteReuseStrategy: that isn't the answer we are looking for.注意:不要建议使用 RouteReuseStrategy:这不是我们正在寻找的答案。 I have another angular application that doesn't reload the component between routes.我有另一个 angular 应用程序,它不会在路由之间重新加载组件。 And this is the official expected behaviour.这是官方预期的行为。

You need to dive into a RouteReuseStrategy and create your custom strategy with saving touched pages Docs您需要深入RouteReuseStrategy并创建自定义策略并保存触摸页面Docs

RouteReuseStrategy allows you to tell Angular not to destroy a component, but in fact to save it for re-rendering at a later date. RouteReuseStrategy允许您告诉 Angular 不要破坏组件,而是将其保存以供以后重新渲染。 https://stackoverflow.com/a/41515648/15439733 https://stackoverflow.com/a/41515648/15439733

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM