Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different layouts for Login Page (one for login and other layout for others) in Angular 2

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?

like image 555
Diego Unanue Avatar asked Sep 12 '16 21:09

Diego Unanue


People also ask

How can we reuse common layouts in Angular using router?

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> .

Can we use multiple router outlet in Angular?

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.

What is layout in Angular?

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.


1 Answers

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.

like image 139
Diego Fontenelle Avatar answered Nov 11 '22 02:11

Diego Fontenelle