刷新时未获取“路由器事件”

use*_*080 5 angular angular-router angular8

我正在为我的项目做面包屑。我这里有两个问题:

  1. 当我来自其同级页面时,未生成面包屑
  2. 页面刷新时未生成面包屑

两者该如何解决呢?

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, RoutesRecognized, NavigationStart,  NavigationEnd, Params, PRIMARY_OUTLET } from '@angular/router';
import { filter, map } from 'rxjs/operators';
import { BreadCrumbsService } from './../../services/bread-crumbs.service';


@Component({
    selector: 'bread-crumbs',
    templateUrl: './bread-crumbs.component.html',
    styleUrls: ['./bread-crumbs.component.scss']
})
export class BreadCrumbsComponent implements OnInit {

    breadcrumbList: Array<any> = [];

    /**
        * Setting Breadcrumb object, where name for display, path for url match,
        link for heref value, getting datas from BreadCrumbsService service
    */

    constructor(private router: Router, private bCService: BreadCrumbsService) {

        this.generateBreadCrumb();

    }

    ngOnInit() {}

    generateBreadCrumb() {

        console.log('hi');

        let routerUrl: string, routerList: Array<any>, target: any;

        this.router.events.subscribe((router: any) => {

            console.log('do'); //not consoles on above 2 points

            routerUrl = router.urlAfterRedirects;


        });

    }

}
Run Code Online (Sandbox Code Playgroud)

Wor*_*arm 1

使用的路由器订阅逻辑 i\xc2\xb4m 更具限制性并且有效:

\n\n

\r\n
\r\n
constructor(private router: Router) { \r\n     router.events.subscribe(event => {\r\n            if (event instanceof NavigationEnd) {\r\n                console.log(event.urlAfterRedirects);\r\n            }\r\n          });\r\n}
Run Code Online (Sandbox Code Playgroud)\r\n
\r\n
\r\n

\n\n

所以我认为你的问题是由你设置路由器模块的方式引起的。\n你是否通过在两个 URL 中使用两个组件来测试你的路由器是否工作?\n或者你会与我们分享你的路由器模块吗?

\n