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.
  1. ng g c Your-Component-Name
From the above syntax, I am creating components.
Components, Router Outlet, Menus & Button click event in Angular 6
After successful creation, each entry gets added into an app.module.ts file. For more information, see the below file.
app.module.ts
  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { AppComponent } from './app.component';
  4. import { HeaderComponent } from './Component/header/header.component';
  5. import { FooterComponent } from './Component/footer/footer.component';
  6. import { CsharpComponent } from './Component/csharp/csharp.component';
  7. import { MvcComponent } from './Component/mvc/mvc.component';
  8. import { ContactusComponent } from './Component/contactus/contactus.component';
  9. @NgModule({
  10. declarations: [
  11. AppComponent,
  12. HeaderComponent,
  13. FooterComponent,
  14. CsharpComponent,
  15. MvcComponent,
  16. ContactusComponent
  17. ],
  18. imports: [
  19. BrowserModule
  20. ],
  21. providers: [],
  22. bootstrap: [AppComponent]
  23. })
  24. export class AppModule { }
Step 2
Now, add the below line in app.module.ts.
  1. 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
  1. import { BrowserModule } from '@angular/platform-browser';
  2. import { NgModule } from '@angular/core';
  3. import { AppComponent } from './app.component';
  4. import { RouterModule } from '@angular/router';
  5. import { HeaderComponent } from './Component/header/header.component';
  6. import { FooterComponent } from './Component/footer/footer.component';
  7. import { CsharpComponent } from './Component/csharp/csharp.component';
  8. import { MvcComponent } from './Component/mvc/mvc.component';
  9. import { ContactusComponent } from './Component/contactus/contactus.component';
  10. @NgModule({
  11. declarations: [
  12. AppComponent,
  13. HeaderComponent,
  14. FooterComponent,
  15. CsharpComponent,
  16. MvcComponent,
  17. ContactusComponent
  18. ],
  19. imports: [
  20. BrowserModule,
  21. RouterModule.forRoot([
  22. { path: 'csharp', component: CsharpComponent },
  23. {path: 'mvc', component: MvcComponent},
  24. {path: "contactus" , component:ContactusComponent}
  25. ]),
  26. ],
  27. providers: [],
  28. bootstrap: [AppComponent]
  29. })
  30. export class AppModule { }
Step 3
Now, to redirect the appropriate component, use the below syntax and add this to the app.component.html page.
  1. <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
  1. <app-header></app-header>
  2. <router-outlet></router-outlet>
  3. <app-footer></app-footer>
Step 4
In each component, I am writing some dummy data:
csharp.component.html
  1. <p>
  2. csharp works!
  3. </p>
mvc.component.html
  1. <p>
  2. mvc works!
  3. </p>
contactus.component.html
  1. <p>
  2. contact us works!
  3. </p>
For a better look & feel, I am going to add some CSS in each component for the paragraph tag.
csharp.component.css
  1. p {
  2. padding: 20px;
  3. width: 98%;
  4. background-color: #dae246;
  5. height: 120px;
  6. }
To see the folder structure of our project, refer to the below image.
Components, Router Outlet, Menus & Button click event in Angular 6
Now, run the application.
  1. ng serve -o
Your output will look like this.
Components, Router Outlet, Menus & Button click event in Angular 6
Now, click on the C# menu and you will get the output as below.
Components, Router Outlet, Menus & Button click event in Angular 6
Now, click on the ASP.NET MVC menu and you will get the output as:
Components, Router Outlet, Menus & Button click event in Angular 6
PART 2 - Show a menu list on mouse over and redirect to components on menu click
Step 1
As we have header.component.html, I am going to create a dropdown list which contains menus. The user would like to open the drop-down menu list on mouseover of the button.
Below is the code to create a drop-down list in the header.component.html file.
Note
I have commented the old code for the time being.
Here is the code of the complete header.component.html file:
  1. <div style="text-align:Left">
  2. <img width="200" alt="CodderFunda Logo" src="http://coderfunda.com/wp-content/uploads/2018/02/logo2.png">
  3. </div>
  4. <!-- <ul>
  5. <li><a routerLink="/csharp">C#</a></li>
  6. <li><a routerLink="/mvc">ASP.NET MVC</a></li>
  7. <li><a routerLink="/contactus">Contact</a></li>
  8. </ul> -->
  9. <h2>Hoverable Dropdown</h2>
  10. <p>Move the mouse over the button to open the dropdown menu list.</p>
  11. <div class="dropdown">
  12. <button class="dropbtn">Menu List</button>
  13. <div class="dropdown-content">
  14. <a routerLink="/csharp">C#</a>
  15. <a routerLink="/mvc">ASP.NET MVC</a>
  16. <a routerLink="/contactus">Contact</a>
  17. </div>
  18. </div>
  19. <p style="margin-top:15%"></p>
Step 2
In header.component.css, by using a class, we are applying CSS and the mouseover effect.
  1. .dropbtn {
  2. background - color: #4CAF50;
  3. color: white;
  4. padding: 16px;
  5. font-size: 16px;
  6. border: none;
  7. }
  8. .dropdown {
  9. position: relative;
  10. display: inline-block;
  11. }
  12. .dropdown-content {
  13. display: none;
  14. position: absolute;
  15. background-color: # f1f1f1;
  16. min - width: 160 px;
  17. box - shadow: 0 px 8 px 16 px 0 px rgba(0, 0, 0, 0.2);
  18. z - index: 1;
  19. }.dropdown - content a {
  20. color: black;
  21. padding: 12 px 16 px;
  22. text - decoration: none;
  23. display: block;
  24. }.dropdown - content a: hover {
  25. background - color: #ddd;
  26. }.dropdown: hover.dropdown - content {
  27. display: block;
  28. }.dropdown: hover.dropbtn {
  29. background - color: #3e8e41;}
Step 3
Now, run the application.
Components, Router Outlet, Menus & Button click event in Angular 6
Now, take the mouse over to the menu. You will get the result as:
Components, Router Outlet, Menus & Button click event in Angular 6
Now, click on the C# menu and you will get an output as -
Components, Router Outlet, Menus & Button click event in Angular 6
PART 3 - Load specific component on button click event.
Step 1
Now, I am creating one component, i.e., Home. Use the following syntax.
  1. ng g c Home
In home.component.html, create one input button with an onclick event as below.
home.component.html
  1. <p> home works! </p>
  2. <input type="button" value="Go To MVC" (click)="onSubmit()" />
  3. <br>
  4. <p>
  5. </p>
Step 2
In the home.component.ts file, write the function for onclick event which will navigate to another component using a router:
  1. import {Router} from '@angular/router';
After that, create an object of the router into a constructor.
Here is the complete code for the home.component.ts file.
  1. import {
  2. Component,
  3. OnInit
  4. } from '@angular/core';
  5. import {
  6. Router
  7. } from '@angular/router';
  8. @Component({
  9. selector: 'app-home',
  10. templateUrl: './home.component.html',
  11. styleUrls: ['./home.component.css']
  12. })
  13. export class HomeComponent implements OnInit {
  14. constructor(private router: Router, ) {}
  15. onSubmit() {
  16. this.router.navigate(['/mvc'])
  17. }
  18. ngOnInit() {}
  19. }
Step 3
Now, run the application and call the home component.
Components, Router Outlet, Menus & Button click event in Angular 6
Click on "Go To MVC" button and it will redirect you to the MVC component.
Components, Router Outlet, Menus & Button click event in Angular 6
Summary
In this article, you learned how to create components, display a component using router outlet, show a menu list on mouseover, and load component on button click event.