Multi-layout in Angular5

How to implement a multi-layout feature in Angular5. A common use case for this is when you have a different layout for your public and secu...

How to implement a multi-layout feature in Angular5. A common use case for this is when you have a different layout for your public and secured pages. For example, your secured page could have a menu on the left side. Or you have a page that doesn't require a layout.

Let's provide some examples. Let's say you have the following requirements:
  1. Plain pages that don't require any layout.
  2. Public pages.
  3. Secured pages.
The core feature that we need to set is the router configuration. 
  1. First, the app.component content must only be the router-outlet tag.
  2. We need to create a module for the layout components. Why we need a separate module for the layout? It's because it's possible to use the layout on different modules. If we just make it part of a module, say app.module, then we cannot use it inside secret.module. 
The layout module will contain the public and secured layout components. These 2 components are just ordinary component with template defined in its HTML page. The main point here is that inside, HTML  tag must be defined. Remember we have another router in the app.component? The Router class has a way of dealing with this, by using the children property.

In this section, we will provide an example of how a route should be defined in app-routing.

Public pages layout:

{
path: '',
component: PublicPageLayoutComponent,
children: [
{ path: '', component: HomeComponent, pathMatch: 'full' },
{ path: 'home', component: HomeComponent },
{ path: 'about', component: AboutComponent}
]
}

Secured pages layout:

{
path: '',
component: SecuredSiteLayoutComponent,
children: [
{ path: '', component: AdminComponent, pathMatch: 'full' },
{ path: 'admin', component: AdminComponent}
]
}

Now, what if we have a lazy loaded module route? If we defined it like below, it will throw an error.

{
path: 'secret',
component: SecuredSiteLayoutComponent,
loadChildren: 'app/module/secret/secret.module#SecretModule'
}

To fix this, we must define a secret-routing.module and defines some routes similar to app-routing.

const routes: Routes = [
{
path: '',
component: SecuredSiteLayoutComponent,
children: [
{ path: '', redirectTo: 'dashboard', pathMatch: 'full' },
{ path: 'dashboard', component: DashboardComponent }
]
},
{
path: 'postRegistration',
component: SecuredSiteLayoutComponent2,
children: [
{ path: '', component: PostRegistrationComponent }
]
}
];

Basically, following the same logic of using the children property.

As a bonus, we are adding a guard that navigates depending on the role of a newly registered user. I used this to redirect the user to a configuration page.
@Injectable()
export class RegistrationGuard implements CanActivate {

constructor( private router: Router, private route: ActivatedRoute ) {

}

canActivate( next: ActivatedRouteSnapshot, state: RouterStateSnapshot ): Observable | Promise | boolean {
console.log( 'registration.guard' )

if ( !KeycloakService.auth.loggedIn || !KeycloakService.auth.authz.authenticated ) {
return false;
}

//check group
if ( KeycloakService.hasGroup( 'Bride' ) ) {
this.router.navigate( ['/bride/postRegistration'] );

} else if ( KeycloakService.hasGroup( 'Vendor' ) ) {
this.router.navigate( ['/bride/postRegistration'] );
}

return true;
}
}
Here's a link on how I secured the pages: http://toztech.blogspot.com/2017/11/secure-angular4-with-keycloak-role-or.html.

COMMENTS

mas template
Name

amazon,1,angular,8,bigdata,2,business,1,course-spring,27,courses,6,database,4,docker,3,java,50,kafka,1,keycloak,4,microservices,5,mysql,1,neworking,1,nosql,2,php,1,pinned,2,react,3,server management,7,shared drive,1,spring,7,synology,1,troubleshooting,2,web,1,wordpress,1,
ltr
item
toztech: Multi-layout in Angular5
Multi-layout in Angular5
toztech
https://toztech.blogspot.com/2018/05/multi-layout-in-angular5.html
https://toztech.blogspot.com/
https://toztech.blogspot.com/
https://toztech.blogspot.com/2018/05/multi-layout-in-angular5.html
true
2554149350007112447
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content