简体   繁体   English

Angular 延迟加载的组件在导航时重新渲染

[英]Angular Lazy Loaded Component Re-renders on navigation

Our application is setup so users navigating to a certain set of routes are lazy loaded like below:我们的应用程序已设置好,因此导航到一组特定路线的用户会延迟加载,如下所示:

{
  path: path1, 
  component: Cmp1,
  loadChildren: () => LazyLoadedModule,
}

We use a router link to navigate to one of these lazy loaded components like this...我们使用路由器链接导航到这些延迟加载的组件之一,如下所示......

<tile
    routerLink="path1/{{name}}/achildpath"
    [state]=someData>
    ...
</tile>

I notice when clicking on this tile the path + lazy loaded component attempts to load.我注意到单击此图块时,路径+延迟加载的组件会尝试加载。 The correct state is passed in initially.最初传入的是正确的 state。 However, the component is destroyed immediately and then the component loads again with the state undefined.但是,该组件会立即被销毁,然后该组件会在未定义 state 的情况下再次加载。 (loads without the state data but loads the correct component)? (加载没有 state 数据但加载正确的组件)? The question is why is the component reloading and destroying itself on first render?问题是为什么组件在第一次渲染时会重新加载和销毁自己? Could this be a problem with the router outlet or the lazy loaded components.这可能是路由器插座或延迟加载组件的问题。 Our router outlet for the lazy loaded component is defined in Cmp1.我们的延迟加载组件的路由器出口在 Cmp1 中定义。

ngAfterViewInit is never called before getting destroyed ngAfterViewInit never gets called before destroy. ngAfterViewInit is never called before getting destroyed之前永远不会被调用 ngAfterViewInit 在销毁之前永远不会被调用。 OnDestroy is what gets called before destroy. OnDestroy是在销毁之前调用的。 But I think you mean why ngAfterViewInit never gets called before comp is destroyed.但是我认为您的意思是为什么在销毁 comp 之前从未调用 ngAfterViewInit 。 It can happen when comp is destroyed before cycle reaches there, or it's being called twice etc. https://blog.logrocket.com/angular-lifecycle-hooks/ Although without seeing rest of the code, an exact problem is just a guess for now.当 comp 在循环到达那里之前被破坏,或者它被调用两次等时,可能会发生这种情况。 https://blog.logrocket.com/angular-lifecycle-hooks/虽然没有看到代码的 rest,但确切的问题只是猜测目前。

Unless tile is an component, you can't use Input/Output to transfer data like that.除非tile是一个组件,否则您不能使用 Input/Output 来传输这样的数据。 @Input() decorator should use at selector, in your case it should be something like: @Input() 装饰器应该在选择器处使用,在你的情况下它应该是这样的:

<app-cmp1 [state]="someData"></app-cmp1>

I am not entirely sure, but I think you can't share data like that between parent->child though routing.我不完全确定,但我认为你不能通过路由在父->子之间共享这样的数据。 You could - (like I showed above) directly from parent->child but not via router.你可以——(就像我上面展示的那样)直接从父级->子级而不是通过路由器。 I would suggest to move data transfer/management to a service instead.我建议将数据传输/管理移至服务。

So regarding your main problem I hope my explanation is enough.所以关于你的主要问题,我希望我的解释就足够了。 Here is a working repo with a service instead.这是一个带有服务的工作仓库。 https://stackblitz.com/edit/angular-pqrqvx?file=src%2Fapp%2Forders%2Forders.component.ts https://stackblitz.com/edit/angular-pqrqvx?file=src%2Fapp%2Forders%2Forders.component.ts

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

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