2

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>

?

2
  • Possible duplicate of: stackoverflow.com/questions/41580495/… Commented Mar 20, 2019 at 13:43
  • Import Router from @angular/router, check current route like this: this.router.url == 'start' ... check against the path url not the module name. Commented Mar 20, 2019 at 23:44

2 Answers 2

4
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']);
            }
        );
    
Sign up to request clarification or add additional context in comments.

1 Comment

You can ---> import Router from @angular/router
0

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
    }
});}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.