在这里:https : //stackblitz.com/edit/planificador?file=src/app/partehoras/components/detail/detail.component.html
加载的第一个路由是这个
在 app.module 我有这个
在 partehoras.module 我有这个
当我点击这里
我走这条路
但是当我刷新这个页面时,我又回到了这个页面
有什么想法吗?
谢谢
主要问题在于main.component.ts
:
@Component(...)
export class MainComponent implements OnInit {
...
ngOnInit(): void {
...;
this.sharedDataService
.getEmpleado(this.empleadoID)
.pipe()
.subscribe(data => {
this.nombreEmpleado = data.nombre;
console.log('Empleado', this.nombreEmpleado);
this.router.navigate(['/partehoras', this.empleadoID]); // Problem here!
},
err => console.log(err),
() => {})
}
}
现在,为什么这条线有问题?
Angular 开始加载应用程序中的组件并解析它们的路由。这些组件之一是您拥有的MainComponent
和它的ngOnInit
方法:
this.router.navigate(['/partehoras', this.empleadoID]);
This means that once the subscribe emits information, you will perform a redirect if this component is appearing in the screen.
Since this component contains the DetailComponent
via the <router-outlet></router-outlet>
this means when you want to load the DetailComponent
you must always pass by it first.
Under normal navigation this is not a problem, but when we refresh, we trigger the ngOnInit
method, which prevents us from ever reaching the DetailComponent
.
There is no simply resolution to this. The problem is you are performing a redirect in the ngOnInit
method, which is usualy a bad approach. Every time the component is created, a redirect will occur. This includes refresh events and possibly other types outside of normal navigation.
我会考虑创建一个警卫来检查何时需要重定向。但是,这取决于您的应用程序的逻辑,因为在这种情况下,我无能为力。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句