Introduction
In this second part of my Angular 6 article series, we are going to learn how to create components, display a component using router outlet, show a menu list on mouseover, and load a component on a button click event.
Before moving to the environment set up, please read my previous article:
PART 1 - How to create components and display a component using router outlet.
Step 1
Now, right-click on the components folder, click on "Open in terminal". Now, your terminal opens in Visual Studio Code. I am going to create three different components for three different menus, i.e., C#, ASP.NET MVC, Contact Us like csharp, mvc, contactus.
To create a component, use the below syntax.
- ng g c Your-Component-Name
From the above syntax, I am creating components.
After successful creation, each entry gets added into an app.module.ts file. For more information, see the below file.
app.module.ts
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { AppComponent } from './app.component';
- import { HeaderComponent } from './Component/header/header.component';
- import { FooterComponent } from './Component/footer/footer.component';
- import { CsharpComponent } from './Component/csharp/csharp.component';
- import { MvcComponent } from './Component/mvc/mvc.component';
- import { ContactusComponent } from './Component/contactus/contactus.component';
- @NgModule({
- declarations: [
- AppComponent,
- HeaderComponent,
- FooterComponent,
- CsharpComponent,
- MvcComponent,
- ContactusComponent
- ],
- imports: [
- BrowserModule
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
Step 2
Now, add the below line in app.module.ts.
- import { RouterModule } from '@angular/router';
Router imports
The Angular Router is an optional service that presents a particular component view for a given URL. It is not part of the Angular Core. It is in its own library package, @angular/router. Import what you need from it as you would from any other Angular package.
Also, import the path and component names as below.
complete app.module.ts file
- import { BrowserModule } from '@angular/platform-browser';
- import { NgModule } from '@angular/core';
- import { AppComponent } from './app.component';
- import { RouterModule } from '@angular/router';
- import { HeaderComponent } from './Component/header/header.component';
- import { FooterComponent } from './Component/footer/footer.component';
- import { CsharpComponent } from './Component/csharp/csharp.component';
- import { MvcComponent } from './Component/mvc/mvc.component';
- import { ContactusComponent } from './Component/contactus/contactus.component';
- @NgModule({
- declarations: [
- AppComponent,
- HeaderComponent,
- FooterComponent,
- CsharpComponent,
- MvcComponent,
- ContactusComponent
- ],
- imports: [
- BrowserModule,
- RouterModule.forRoot([
- { path: 'csharp', component: CsharpComponent },
- {path: 'mvc', component: MvcComponent},
- {path: "contactus" , component:ContactusComponent}
- ]),
- ],
- providers: [],
- bootstrap: [AppComponent]
- })
- export class AppModule { }
Step 3
Now, to redirect the appropriate component, use the below syntax and add this to the app.component.html page.
- <router-outlet></router-outlet>
Router outlet
The RouterOutlet is a directive from the router library that is used as a component. It acts as a placeholder that marks the spot in the template where the router should display the components for that outlet.
complete app.component.html file
- <app-header></app-header>
- <router-outlet></router-outlet>
- <app-footer></app-footer>
Step 4
In each component, I am writing some dummy data:
csharp.component.html
- <p>
- csharp works!
- </p>
mvc.component.html
- <p>
- mvc works!
- </p>
contactus.component.html
- <p>
- contact us works!
- </p>
For a better look & feel, I am going to add some CSS in each component for the paragraph tag.
csharp.component.css
- p {
- padding: 20px;
- width: 98%;
- background-color: #dae246;
- height: 120px;
- }
To see the folder structure of our project, refer to the below image.

Now, run the application.
- ng serve -o
Your output will look like this.

Now, click on the C# menu and you will get the output as below.


a agaPosted Sep 11, 2020, 8:36 AM
Router link not working page is not display
Amit Kumar SinghPosted Oct 8, 2018, 6:55 AM
Nice one ... Thanks for Sharing Rupesh Sir :)