knnhcn
knnhcn

Reputation: 1151

Lazy loading get current routed module

I am trying to exclude a component if a certain module has been routed in a lazy loading application.

For example in my AppComponent i am using router-outlet and above a component:

<div>
    <my-component></my-component>   --> don't show if module is StartModule
    <router-outlet></router-outlet>
</div>

My routing configuration looks like following:

export const routes: Routes = [
  {
    path: 'start',
    loadChildren: './start/start.module#StartModule',
  },
  {
    path: 'first',
    loadChildren: './first/first.module#FirstModule'
  },
  {
    path: 'second',
    loadChildren: './second/second.module#SecondModule'
  }
];

Is there a parameter to receive the routed module to make a check like

isStartModule(): boolean {
    if (routedModule == StartModule) {
        return true; 
    }
}

<my-component *ngIf="!isStartModule()"></my-component>

?

Upvotes: 2

Views: 1558

Answers (2)

Boris Adamyan
Boris Adamyan

Reputation: 378

constructor(private router: Router ) {}

try to check

this.router.url === '/start' 

then do something


  1. You can sucscribe to event

     this.router.events.pipe(
        filter((event) => event instanceof NavigationEnd))
        .subscribe(x => {
            console.log('THIS IS FOR TEST', x['url']);
            }
        );
    

Upvotes: 4

Murtuza
Murtuza

Reputation: 382

You can set a route change listener in your app-component like this:

import { Router, NavigationEnd } from '@angular/router';
import { filter } from 'rxjs/operators';
constructor(private router: Router)
{
  router.events.pipe(
  filter(event => event instanceof NavigationEnd) ).subscribe((event: NavigationEnd) => {    
    console.log(event.url);
    if(event.url == 'start'){
    // do you stuff
    }
});}

Upvotes: 0

Related Questions