Reputation: 1151
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
Reputation: 378
constructor(private router: Router ) {}
try to check
this.router.url === '/start'
then do something
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
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