以下是我组件的代码:
import { Component } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
showMenu:any='';
constructor(router:Router) {
router.events.forEach((event) => {
if(event instanceof NavigationStart) {
this.showMenu = event.url !== "/newsection";
console.log(event.url);
}
});
}
}
我遇到了错误:
ReferenceError:未定义NavigationStart
答案 0 :(得分:0)
import { Component } from '@angular/core';
import { Router, ActivatedRoute,NavigationStart } from '@angular/router';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
showMenu:any='';
constructor(router:Router) {
router.events.forEach((event) => {
if(event instanceof NavigationStart) {
this.showMenu = event.url !== "/newsection";
console.log(event.url);
}
});
}
}
答案 1 :(得分:0)
替换:
import { Router, ActivatedRoute } from '@angular/router';
作者:
import { Router, NavigationStart } from '@angular/router';
然后您可以像这样订阅:
router.events.subscribe(event => {
if(event instanceof NavigationStart) {
// ...
}
}