I creating an angular 2 application. In my app.component I've got this
<header-component><header-component>
<router-outlet></router-outlet>
with the router:
const appRoutes: Routes = [
{ path: 'profesionals', component: CrearEditarProfesionalsComponent },
{ path: 'home', component: HomeComponent },
{ path: 'login', component: LoginComponent },
{ path: '', component: HomeComponent }
];
when I navigate to login I want to get rid of the
<header-component>
but for all other pages when the user is logged in I just want to display the layout as it is. Do I have to use ng-if with a service to archieve this? What's the best way?
Option 2 — use lazy loaded modules Define layout as a component in a separate module with routing. Let's call that module LayoutModule . Define all feature modules as lazy loaded children modules inside LayoutModule . Again, in the root component template ( AppComponent ) use only <router-outlet> .
You can add multiple outlets in your Angular application which enables you to implement advanced routing scenarios. Any component that gets matched by the Router will render it as a sibling of the Router outlet.
The layout API consists of a set of Angular directives that can be applied to any of your application's HTML content. Using HTML Directives as the API provides an easy way to set a value (eg. layout="row" ) and helps with separation of concerns: Attributes define layout while CSS classes assign styling.
You can remove the from you page. For your desired result to be achieved you can make all the components that should display the header-component as a child route of it.
You can achieve it like this:
{ path: 'dashboard', component: HeaderComponent, children: [
{ path: '', component: HeaderComponent },
{ path: 'mypath', component: MyComponent } ]
},
{ path: 'login', component: LoginComponent }
This way every route that is a child of the HeaderComponent route will display the HeaderComponent and the child component.
And your login will not display the HeaderComponent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With