Showing posts with label angular-layouts. Show all posts
Showing posts with label angular-layouts. Show all posts

Monday, September 4, 2023

How to Create Multi Layout Application with Angular | Angular - Reusable Layout Templates Using Angular Routing | Angular material layout

Angular material provides a layout to create our component inside it; Layout is basically a directive which helps to show or represent the layout for its children. We basically have two types of layouts: flex and grid; angular material provides us inbuild module and directive to create this layout in our application. We all know about the grid layout, and it is very easy to create as well, by the use of a few configurations. We also have few properties that are used to assign the Layout values, and it is mainly as ‘row’ and ‘column’; we can set this parameter some value. Also, we can make the layout responsive by using the Layout property which it is provided. In the coming section of the tutorial, we will have a closer look at the implementation and what changes need to be made in order to run our application properly with detailed examples for beginners.
First, install the Angular CLI, which enables us to download the required packages and library for our project. You can download it by typing the below command on your command make sure you have already installed node see below;
Run command: npm install -g @angular/cli
The above command will install the CLI globally in our system; hence we can use it globally when required.

Now, in this step, we will try to create the new angular project from scratch using routing module using below command

ng new ang1 --routing --defaults

Navigate to project directory then just to make sure, try one command which is mentioned below to install all the required library into our project
npm install

Will will run below command to run our first angular project:
ng serve

Out first project is running:
We will continue our second part where we will manage layouts for different types of page, suppose login page will be a clean page where home page page as well as other pages will have left menu enabled. So we will create first layouts here. 1. Default Layout and 2. Static Layout

To do so we will run below commands to create layouts, these 2 commands with create two layouts default and static:
ng g c layouts/default
ng g m layouts/default
ng g c layouts/static
ng g m layouts/static

Open layouts/static.module.ts and replace StaticModule with StaticLayoutModule, do same for layouts/default.module.ts, replace DefaultModule to DefaultLayoutModule

Open layouts/static.component.ts and replace StaticComponent with StaticLayoutComponent, do same for layouts/default.component.ts, replace DefaultComponent to DefaultLayoutComponent

We will create our componets home and login using below command:
ng g c modules/home
ng g c modules/login
We will keep app.module.ts as much as simple like below, there will be references to Layouts only rather than components:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import {DefaultLayoutModule} from "./layouts/default/default.module";
import {StaticLayoutModule} from "./layouts/static/static.module";

@NgModule({
    declarations: [
        AppComponent,
    ],
    imports: [
        BrowserModule,
        AppRoutingModule,

        DefaultLayoutModule,
        StaticLayoutModule,
    ],
    providers: [],
    bootstrap: [AppComponent]
})
export class AppModule { }

Contents of app/app.component.html:
<router-outlet></router-outlet>

Contents of app-routing.module.ts:
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import {HomeComponent} from "./modules/home/home.component";
import {DefaultLayoutComponent} from "./layouts/default/default.component";
import {LoginComponent} from "./modules/login/login.component";
import {StaticLayoutComponent} from "./layouts/static/static.component";

const routes: Routes = [
    {
        path: '',
        component: DefaultLayoutComponent,
        children: [
            {
                path: '',
                component: HomeComponent,
            }
        ]
    }, {
        path: '',
        component: StaticLayoutComponent,
        children: [
            {
                path: 'login',
                component: LoginComponent,
            }
        ]
    },
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  exports: [RouterModule]
})
export class AppRoutingModule { }

We will create shared component header and footer so that we can use them from any of your component, use below command to create shared components:
ng g c shared/header
ng g c shared/footer

Shared components are created, now we will create shared module using below command:
ng g m shared/

We will modify our shared module as below (app/shared/shared.module.ts):
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {HeaderComponent} from "./header/header.component";
import {FooterComponent} from "./footer/footer.component";
import {RouterModule} from "@angular/router";

@NgModule({
    declarations: [
        HeaderComponent,
        FooterComponent,
    ],
    imports: [
        CommonModule,
        RouterModule,
    ],
    exports: [
        HeaderComponent,
        FooterComponent,
    ]
})
export class SharedModule { }

Contents of app/shared/header/header.component.html:
<header>
    <div>
        <a routerLink="/">Home</a>
        <a routerLink="/login">Login</a>
    </div>
</header>

Contents of app/shared/footer/footer.component.html:
<footer>
    <p>footer works!</p>
</footer>
Lets open app/layouts/default/default.module.ts and keep as below where we import our components (for example home component):
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {HomeComponent} from "../../modules/home/home.component";
import {BrowserModule} from "@angular/platform-browser";
import {FormsModule} from "@angular/forms";
import {RouterModule} from "@angular/router";
import {SharedModule} from "../../shared/shared.module";
import {DefaultLayoutComponent} from "./default.component";

@NgModule({
    declarations: [
        DefaultLayoutComponent,
        HomeComponent, //register home component will use this layout
    ],
    imports: [
        FormsModule,
        CommonModule,
        BrowserModule,
        RouterModule,
        SharedModule,//we will access header and footer via shared module
    ]
})
export class DefaultLayoutModule { }

Contents of default.component.html:
<div class="container-fluid">
    <div class="container-fluid">
        <header class="row">
            <app-header></app-header>
        </header>
        <div id="main" class="row">
            <router-outlet></router-outlet>
        </div>
        <footer class="row">
            <app-footer></app-footer>
        </footer>
    </div>
</div>


Lets open app/layouts/static/static.module.ts and keep as below where we import our components (for example login component):
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import {StaticLayoutComponent} from "./static.component";
import {LoginComponent} from "../../modules/login/login.component";
import {FormsModule} from "@angular/forms";
import {BrowserModule} from "@angular/platform-browser";
import {RouterModule} from "@angular/router";
import {SharedModule} from "../../shared/shared.module";

@NgModule({
    declarations: [
        StaticLayoutComponent,
        LoginComponent, //register login component will use this layout
    ],
    imports: [
        FormsModule,
        CommonModule,
        BrowserModule,
        RouterModule,
        SharedModule,//we will access header and footer via shared module
    ]
})
export class StaticLayoutModule { }

Contents of static.component.html where we would not include footer and that the way we can differ two layouts default and static:
<div class="container-fluid">
    <div class="container-fluid">
        <header class="row">
            <app-header></app-header>
        </header>
        <div id="main" class="row">
            <router-outlet></router-outlet>
        </div>
    </div>
</div>
Lets check what we did so far, run command ng serve to start application, below is screenshot of both home and login page where we can clearly seen two different layouts are using.

Home page using default layout where both header and footer available


Login page using static layout where only header available
This is github project link https://github.com/pritomkucse/angular-layout