Почему событие маршрутизатора не показывает маршруты в компоненте и показывает ошибку - PullRequest
0 голосов
/ 02 января 2019

Ниже код моего компонента:

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 не определено

Ответы [ 2 ]

0 голосов
/ 02 января 2019

Заменить:

import { Router, ActivatedRoute } from '@angular/router';

по:

import { Router, NavigationStart } from '@angular/router';

Тогда вы можете подписаться так:

router.events.subscribe(event => {
    if(event instanceof NavigationStart) {
        // ...
    }
}
0 голосов
/ 02 января 2019
    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);
          }
        });
      }
    }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...