Getting multiple items out of this.router.events.subscribe()

Viewed 2807

I am currently subscribing on router events to change the title of my page when routes change, like so

this.routerSub$ = this.router.events
    .filter(event => event instanceof NavigationEnd)
    .map(() => this.activatedRoute)
    .map(route => {
        while (route.firstChild) route = route.firstChild;
        return route;
    })
    .filter(route => route.outlet === 'primary')
    .mergeMap(route => route.data)
    .subscribe((event) => {
        title = event['title']
            ? `${event['title']}`
            : 'Default Title'
        this.title.setTitle(title);
    });

My issue is that on certain scenarios, I want to set the title to a value in url (part of a route). How do I do this here? I know I cannot subscribe to the url as well as events but am having a hard time figuring out what to map inside.

Here is what I am trying that is failing

   this.router.events
        .filter(event => event instanceof NavigationEnd)
        .map(() => {
            return this.activatedRoute
        })
      .switchMap(route => {
        return route.data.combineLatest(route.url, (data, url) => {
            return data['title'] ? `Title: ${data['title']}` : url.join('');
        });
    })

On the .switchMap line, route is of type ActivatedRoute, route.data is of type Object and the error I get on the combineLatest() line is

route.data.combineLatest is not a function

1 Answers
Related