Create Components In Angular 5


We have already seen how to create your first Angular application in my previous article, i.e., Getting started with Angular 5. Now, in this article, we will learn what a component is and how to create components.
What is a component?
  • Every Angular application has at least one component that is used to display the data on the View. 
  • The component contains metadata like animation effect, style to apply the components, template, input-output, import etc.
  • These are the main building blocks of an Angular application.
  • When we create any component, it gets defined in NgModule.
  • Suppose you are going to develop a simple application which contains home page, logo - menu list - footer etc.; the blogs page contains blogs list, contact information about the author etc. So we are going to create these components.
Step 1

To create components, go to src/app as you can see the folder structure. Now, by clicking the mouse as shown in the below image, type cmd then press Enter key, so the command prompt will open.
To create different components, use the following syntax.
  1. ng g c YourComponentName  
This will create the component. I am going to create three different components as home, blogs, & contact.

In the home component, I am going to write some HTML code which contains logo & menu list.
In blog & contact, I am going to write just a welcome message to the blog page.
Step 2

In home.component.html, write the below code.
  1. <div style="text-align:Left">  
  2.   <img width="200" alt="CodderFunda Logo" src="">  
  3. </div>  
  4. <h4>Here are sample menu items </h4>  
  5. <ul>  
  6.   <li><a routerLink="#home">Home</a></li>  
  7.   <li><a routerLink="/blogs">Latest Blogs</a></li>  
  8.   <li><a routerLink="/events">Events</a></li>  
  9.   <li><a routerLink="/contact">Contact</a></li>  
  10. </ul>  
The routerLink directives on the anchor tags give the router control over those elements. The navigation paths are fixed.
Step 3

In app.component.html, write your selector & we are going to use routing as below.
  1. <app-home></app-home>  
  2. <router-outlet></router-outlet>   
<router-outlet> is used to specify which comopnent should load. Suppose you are going to access URL localhost:1234/blogs then router matches that URL to route path /blogs and displays the content from blogs.component.html to View.

Step 4

Now, you have to import components into an app.module.ts file as shown in below. Also, specify the entry of each address bar path and the component to render into the router-outlet.
  1. import { BrowserModule } from '@angular/platform-browser';  
  2. import { NgModule } from '@angular/core';  
  4. import { AppComponent } from './app.component';  
  5. import { RouterModule } from '@angular/router';  
  6. import { HomeComponent } from './Component/home/home.component';  
  7. import { BlogsComponent } from './Component/blogs/blogs.component';  
  8. import { enableProdMode } from '@angular/core';  
  9. import { ContactComponent } from './Component/contact/contact.component';  
  10. import { EventsComponent } from './Component/events/events.component';  
  12. @NgModule({  
  13.   declarations: [  
  14.     AppComponent,  
  15.     HomeComponent,  
  16.     BlogsComponent,  
  17.     ContactComponent,  
  18.     EventsComponent  
  19.   ],  
  20.   imports: [  
  21.     BrowserModule,  
  22.     RouterModule.forRoot([  
  23.       { path: 'blogs', component: BlogsComponent },  
  24.       { path: 'contact', component: ContactComponent },  
  25.       { path: 'events', component: EventsComponent }  
  26.     ])  
  27.   ],  
  28.   providers: [],  
  29.   bootstrap: [AppComponent]  
  30. })  
  31. export class AppModule { }  
Step 5

Now, run the application by using the below command.
  1. ng serve -o  
Click on the menu item Latest Blogs.
Now, click on Contact.

In this article, we learned what a component is and how to create a component.