我不太明白 router.event.subscribe 的作用,我正在学习这里的教程 https://coursetro.com/posts/code/154/Angular-6-Tutorial---Learn-Angular-6-in-this-Crash-Course 但是我在这部分出错了'((_: NavigationEnd) => this.currentUrl = _.url) ',有人可以帮我解决这个问题吗?在此期间,请详细说明这条线的作用。非常感谢。
import { Component, OnInit } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
@Component({
selector: 'app-sidebar',
templateUrl: './sidebar.component.html',
styleUrls: ['./sidebar.component.scss']
})
export class SidebarComponent implements OnInit {
currentUrl: any;
constructor(private router: Router) {
router.events.subscribe((_: NavigationEnd) => this.currentUrl = _.url);
}
ngOnInit() { }
}
答案 0 :(得分:0)
路由器有不同的生命周期阶段需要跟进。当路由器初始化或完成它的一个周期时,它通过此事件订阅进行更新。
列表如下:
NavigationStart
:导航开始。
RouteConfigLoadStart
:在路由器延迟加载路由配置之前。
RouteConfigLoadEnd
:延迟加载路由之后。
RoutesRecognized
:当路由器解析 URL 并识别路由时。
GuardsCheckStart
:当路由器开始路由的保护阶段时。
ChildActivationStart
:当路由器开始激活路由的子节点时。
ActivationStart
:当路由器开始激活路由时。
GuardsCheckEnd
:当路由器成功完成路由的守卫阶段。
ResolveStart
:当路由器开始路由的解析阶段时。
ResolveEnd
:当路由器成功完成路由的解析阶段。
ChildActivationEnd
:当路由器完成激活路由的子节点时。
ActivationEnd
:当路由器完成激活路由时。
NavigationEnd
:导航成功结束时。
NavigationCancel
:取消导航时。
NavigationError
:当导航因意外错误而失败时。
Scroll
:当用户滚动时。
查看更多详情here。