How to Know Which component is loaded in router-outlet in Angular 5 and above

后端 未结 2 1658
时光取名叫无心
时光取名叫无心 2021-01-06 06:14

This my ap.component.html




         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-06 06:59

    header.component.html

    routing

    ...
      { path: 'services' , component: ServicesComponent },
      { path: '', pathMatch: 'full', redirectTo: '/' },
      { path: '**', component: NotfoundComponent , data: { status : 'notfound'}}
    ];
    

    header.component.ts

     ngOnInit() {
        this.isNotFound = false;
        this.router.events
            .filter(e => e instanceof NavigationEnd)
            .subscribe(event => {
              this.isNotFound = this.route.firstChild.data._value.status;
              if (this.isNotFound) {
                this.isNotFound = true;
              }
            });
      }
    

    First, I sent data only to the notfound component then received it on header component then using ngClass add a class on the navbar which is outside router outlet.

    This is how i solved it, Though thanks for all the advice. :)

提交回复
热议问题