Установить активную вкладку страницы управления вкладками из пользовательского элемента управления

Добавляя к ответ Калёяна , эта подписка привязана к маршрутизатору и будет жить до тех пор, пока страница не будет полностью обновлена. При подписке на события маршрутизатора в компоненте обязательно отмените подписку в ngOnDestroy:

import { OnDestroy } from '@angular/core';
import { Router, NavigationEnd } from '@angular/router';
import { Subscription } from "rxjs/Rx";

class MyAppComponent implements OnDestroy {

  private subscription: Subscription;

  constructor(router: Router) {
    this.subscription = router.events.subscribe(s => {
      if (s instanceof NavigationEnd) {
        const tree = router.parseUrl(router.url);
        if (tree.fragment) {
          const element = document.querySelector("#" + tree.fragment);
          if (element) { element.scrollIntoView(element); }
        }
      }
    });
  }

  public ngOnDestroy() {
    this.subscription.unsubscribe();
  }
}

0
задан Simon 15 January 2019 в 15:51
поделиться