Access active route params from root component

Viewed 56

I need to get the active router params from the root component, the following doesn't work

import { Component, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute, Event, NavigationEnd, Router, RoutesRecognized } from '@angular/router';
import { Subscription } from 'rxjs';
import { filter, map } from 'rxjs/operators'

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit, OnDestroy {
  title = 'frontend';
  private _owner: string
  private paramMapSub$!: Subscription

  constructor(private route: ActivatedRoute, private router: Router) {
    this._owner = ""
  }


  ngOnInit(): void {
    this.paramMapSub$ = this.router.events.pipe(
      filter(event => event instanceof NavigationEnd),
      map(() => this.route)
    ).subscribe(route => { this._owner = route.snapshot.paramMap.get("owner")! })
  }

  ngOnDestroy(): void {
    this.paramMapSub$.unsubscribe()
  }

  get owner() {
    return this._owner
  }
}

The firstChild attribute is always null and the active route never contains any parameter. Subscribing to the RoutesRecognized event won't help neither.

Angular version 12.

0 Answers
Related