Introduction
In this article, we will learn to make a component and apply a template in Angular 10.
What is a Component?
In Angular, a component encapsulates the data, Html Template, and the logic for the view. In real world applications, we can say components are like navbar, sidebar, header, footer components, etc.
Or we can take it as the ViewComponents of .Net Core MVC.
We can add several components on one view or the different view where we want to use it. While applying the template, we divide the template into several components and add them to the layout view (index.html).
Here, I am going to apply the AdminLTE Layout for the admin panel. you can download the AdminLTE Template from this site,
https://adminlte.io/themes/dev/AdminLTE/index.html
So let's start working on it.
Step 1
Before starting, please make sure you have set up your Angular project and installed all the dependencies. If you haven't, then please follow this article
here.
Step 2
Create an Angular-10 project in .net core MVC, run ng-build command, and generate node_modules folder. For applying the layout, we'll download the admin template from the adminlte.io website. And copy its CSS, js folder, and paste it into the assets folder. Make sure you paste all assets like CSS, js, images, and fonts folder into the src/assets folder. Because this assets folder path has registered in the angular.json file so, Angular quickly identifies these paths.
Make sure that in angular.json file assets folder path is registered with options tag:
- "assets": ["src/assets"],
In Angular, the main root file is the index.html file in the src folder. In this file, we found the HTML, head, body, tags. And the extra one is app-root, which represents the root of the HTML document. We can place our CSS and js files links into this document. But it's not a good practice for a developer. So, we always keep our CSS and js file link from the view page and registered all that files into the angular.json file into Styles and Scripts tags. like this.
In ClientApp/angular.json file,
- "styles": [
- "node_modules/bootstrap/dist/css/bootstrap.min.css",
- "src/styles.css",
- "src/assets/Content/all.min.css",
- "src/assets/Content/adminlte.min.css"
- ],
- "scripts": [
- "node_modules/jquery/dist/jquery.min.js",
- "src/assets/js/bootstrap.bundle.min.js",
- "src/assets/js/adminlte.js"
- ]
app.component.html file,
This file is found in the clientapp/src/app/app.component.html path. It's generally in the body of the document so we can apply the template body classes from here. And that section which we want to render during the development of the website, will be registered here.
We saw RenderBody() section in .net MVC & Core, and Angular provides the <router-outlet></router-outlet> tag for rendering dynamic contents in a block.
index.html
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8" />
- <title>Angular Demo Web Application</title>
- <base href="/" />
-
- <meta name="viewport" content="width=device-width, initial-scale=1" />
- <!-- IonIcons -->
- <link rel="stylesheet" href="http://code.ionicframework.com/ionicons/2.0.1/css/ionicons.min.css">
- <!-- Google Font: Source Sans Pro -->
- <link href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700" rel="stylesheet">
- </head>
- <body>
- <app-root>Loading...</app-root>
- </body>
- </html>
app.component.html
- <body class="hold-transition sidebar-mini">
- <div class="wrapper">
- <app-header></app-header>
- <app-sidebar></app-sidebar>
- <div class="content-wrapper">
- <router-outlet></router-outlet>
- </div>
- <app-footer></app-footer>
- </div>
- </body>
Okay, here I am dividing my layout view into separate components for neat and clean viewing, and it's a good practice also. So here, I am creating three separate components for header, footer, & sidebar. Now let's generate components from the command.
- ng g c new-component --module app
Usually, we will generate components from ng g c new-component command, but here we have to specify the exact path so, we add the module tag, which is the "app" module. Or if you are in the other directory then use this command:
- ng g c component-name --module ../
Now I've registered all these generated components in clientapp/src/app-module.ts.
header.component.html
- <!-- Navbar -->
- <nav class="main-header navbar navbar-expand navbar-dark navbar-primary">
- <!-- Left navbar links -->
- <ul class="navbar-nav">
- <li class="nav-item">
- <a class="nav-link" data-widget="pushmenu" href="#" role="button"><i class="fas fa-bars"></i></a>
- </li>
- <li class="nav-item d-none d-sm-inline-block">
- <a href="index3.html" class="nav-link">Home</a>
- </li>
-
- </ul>
- <!-- SEARCH FORM -->
- <form class="form-inline ml-3">
- <div class="input-group input-group-sm">
- <input class="form-control form-control-navbar" type="search" placeholder="Search" aria-label="Search">
- <div class="input-group-append">
- <button class="btn btn-navbar" type="submit">
- <i class="fas fa-search"></i>
- </button>
- </div>
- </div>
- </form>
- <!-- Right navbar links -->
-
- </nav>
- <!-- /.navbar -->
footer.component.html
- <!-- Main Footer -->
- <footer class="main-footer">
- <strong>Copyright © @DateTime.Now <a href="#">AdminLTE.io</a></strong>
- All rights reserved.
- <div class="float-right d-none d-sm-inline-block">
- <b>Version</b>
- </div>
- </footer>
sidebar.component.html
- <!-- Main Sidebar Container -->
- <aside class="main-sidebar sidebar-dark-primary elevation-4">
- <!-- Brand Logo -->
- <a href="#" class="brand-link navbar-primary">
-
- <img src="assets/img/AdminLTELogo.png" alt="AdminLTE Logo" class="brand-image img-circle elevation-3"
- style="opacity: .8">
- <span class="brand-text font-weight-light">Admin</span>
- </a>
- <!-- Sidebar -->
- <div class="sidebar">
- <!-- Sidebar user panel (optional) -->
- <div class="user-panel mt-3 pb-3 mb-3 d-flex">
- <div class="image">
-
- <img src="assets/img/nouser.png" class="img-circle elevation-2" alt="User Image">
- </div>
- <div class="info">
- <a href="#" class="d-block">Khushbu Saini</a>
- </div>
- </div>
- <!-- Sidebar Menu -->
- <nav class="mt-2">
- <ul class="nav nav-pills nav-sidebar flex-column" data-widget="treeview" role="menu" data-accordion="false">
- <!-- Add icons to the links using the .nav-icon class
- with font-awesome or any other icon font library -->
-
- <li class="nav-item">
- <a href="#" class="nav-link active">
- <i class="nav-icon fas fa-tachometer-alt"></i>
- <p>
- Dashboard
- </p>
- </a>
- </li>
-
- </ul>
- </nav>
- <!-- /.sidebar-menu -->
- </div>
- <!-- /.sidebar -->
- </aside>
Output
Build and run the project by ng-serve command .
Now we can see our admin layout look likes this.
Summary
In the above article, we saw how to apply a custom admin layout with our CSS and js. We learned to create components and add them with templates layout view, and how to add custom CSS and js files.