我对前端的东西比较陌生,我在玩 Angular 6,所以我面临以下组件的问题是 NavigationEnd返回 undefined 而我不知道为什么:
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: string;
constructor(private router: Router) {
router.events.subscribe(
(navEnd: NavigationEnd) => this.currentUrl = navEnd.url
);
}
}
最佳答案
router.events
有不同的事件,如果您需要获取 NavigationEnd 则需要按如下方式进行过滤。试试这个,
router.events
.pipe(filter(e => e instanceof NavigationEnd))
.subscribe((e: NavigationEnd) => {
this.currentUrl = e.url;
});
关于javascript - NavigationEnd 的 url 属性返回未定义(Angular 6),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51573947/