我使用下面的代码获取路由器的当前URL
this.router.events.subscribe(event => {
if (event instanceof NavigationEnd) {
this.currentRoute = event.url;
}
});然后im通过@Input将currentRoute传递给子组件。
<app-actions *ngIf="currentRoute" [currentRoute]="currentRoute"></app-actions>在我的子组件中,我选择了@Input() currentRoute: string;
问题是,第一次呈现组件时,currentRoute是可见的,但是在任何下一个URL更改上,路由器可观察到的路由器都不会为我提供子组件中的最新值。
有什么想法吗?
发布于 2022-08-11 22:26:15
Input确实改变了,您只是没有在app-actions中听它。在子组件中,使用ngOnChanges生命周期挂钩,如下所示
ngOnChanges(changes: SimpleChanges): void {
console.log(changes);
}在您的app-actions组件中执行重新分配操作,即this.currentRoute = changes.currentRoute.currentValue,您很好
https://stackoverflow.com/questions/73327090
复制相似问题