如何在路由器未加载的组件中获取有关路由更改的当前路由参数

时间:2019-11-05 16:22:53

标签: angular angular-router

我正在尝试从看起来像“ / categories /:cat1 /:cat2 / browse”的路由中获取参数

将其添加到由路由加载的组件中似乎可以正常工作,但是应用程序中已经加载且未被路由更改更改的另一个组件

import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute, Params, RoutesRecognized } from '@angular/router';

@Component({
  selector: 'app-breadcrumbs',
  templateUrl: './breadcrumbs.component.html',
  styleUrls: ['./breadcrumbs.component.scss'],
})
export class BreadcrumbsComponent implements OnInit {
  constructor(private activatedRoute: ActivatedRoute, router: Router) {
    router.events.subscribe(e => {
      activatedRoute.params.subscribe(params => {
        console.log({ params });
      });
    });
  }
  ngOnInit() {}
}

2 个答案:

答案 0 :(得分:1)

您不想订阅路由参数,而不是查询参数。 (即,而不是ActivatedRoute.queryParams.subscribe而不是ActivatedRoute.queryParams.subscribe)

答案 1 :(得分:0)

我最终解决此问题的方法是:

1)创建一个使用BehaviorSubject接收参数更新的面包屑服务

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs';
import { BreadcrumbsList } from './breadcrumbsList';
import { ParseErrorLevel } from '@angular/compiler';

@Injectable({
  providedIn: 'root'
})
// https://stackoverflow.com/questions/46047854/how-to-update-a-component-without-refreshing-full-page-angular
export class BreadcrumbsService {
  public breadcrumbsList: BehaviorSubject<BreadcrumbsList> = new BehaviorSubject({
    '@context': 'https://schema.org',
    '@type': 'BreadcrumbList',
    itemListElement: []
  });

  constructor() {}

  // https://developers.google.com/search/docs/data-types/breadcrumb#example
  updateParamMap(paramMap, root, suffix = null, endpoint = null) {
    const breadcrumbsList = {
      '@context': 'https://schema.org',
      '@type': 'BreadcrumbList',
      itemListElement: []
    };
    let position = 0;
    let curPath = '';
    console.log({ test: paramMap.get('level1') });
    for (var i = 0; i < 6; i++) {
      let level = paramMap.get('level' + i);
      if (i === 0) {
        level = root;
      }
      if (!level) {
        continue;
      }
      curPath += '/' + level;
      let item = {
        '@type': 'ListItem',
        position: i + 1,
        name: level,
        item: suffix ? curPath + '/' + suffix : curPath
      };
      breadcrumbsList.itemListElement.push(item);
    }
    if (endpoint) {
      let item = {
        '@type': 'ListItem',
        position: i + 2,
        name: endpoint
      };
      breadcrumbsList.itemListElement.push(item);
    }
    // update the breadcrumbs list for anything that is listening for updates
    this.breadcrumbsList.next(breadcrumbsList);
  }
}

2)在应该更新面包屑的路由所加载的每个组件中,导入面包屑服务,并将此行添加到ngOnInit()

this.breadcrumbsService.updateParamMap(this.route.snapshot.paramMap, 'assets', 'browse');

3)在面包屑组件中,让面包屑列表从面包屑服务订阅更新

import { Component, OnInit, Input } from '@angular/core';
import { BreadcrumbsList } from './breadcrumbsList';

@Component({
  selector: 'app-breadcrumbs',
  templateUrl: './breadcrumbs.component.html',
  styleUrls: ['./breadcrumbs.component.scss']
})
export class BreadcrumbsComponent implements OnInit {
  @Input() path: String[];
  breadcrumbsList: BreadcrumbsList;

  constructor(breadcrumbsService: BreadcrumbsService) {
    breadcrumbsService.breadcrumbsList.subscribe(value => {
      this.breadcrumbsList = value;
    });
  }

  ngOnInit() {}
}

面包屑模板可以处理BreadcrumbsList