Creating Our First Component In Angular 2 - Part Five

Hi friends!

From our previous article, we have learned that Angular applications are made of several components each representing its own View. Refer to my previous article for better understanding.

And, we learned about app.component which is responsible for displaying the content in our browser. Now, in this article, we shall see how to create our own component in Angular 2 applications.

The first thing we have to do is to create new TypeScript file.


I have added a file called rathrola.component.ts. Just like our app.component, we have imported the component class from core module, as shown below.

  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. @Component({  
  5.     selector: "my-tuts",  
  6.     template: `<h2>Rathrola Prem Kumar, Consultant</h2>`,  
  7. })  
  8. export class RathrolaComponent {}  

If you remember, a component is nothing but a class with metadata. Let’s name our class as rathrolaComponent and since we need to use this class in other components, we are going to export it.

And now, we shall attach the component decorator of our class. Within this component decorator, we are going to specify a selector and a template as shown below.

  1. @Component({  
  2.     selector: "my-tuts",  
  3.     template: `<h2>Rathrola Prem Kumar, Consultant</h2>`,  
  4. })  

In template, we created simple html tags as shown above.

Now, we have an import statement, component decorator, and associated class. Finally, our newly created component is complete.

In my previous article, I have mentioned that usually in Angular application, we will have one main component called root component and other components are part of this root component.

In our case, root component is app.component and rathrola.component is going to live inside this app.component.

Hope, you are very clear.

So now, from app.component, remove the single line quotes in template in component decorator, as shown below.

  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. @Component({  
  5.     selector: 'my-app',  
  6.     template: `<h1>Hello World From Rathrola Prem Kumar</h1>`,  
  7. })  
  8. export class AppComponent {}  

So, remove the single quotes and keep back tick (`). This will be below the esc key in the keyboard.

This is going to allow us to specify html in multiple lines. Now, what is the html we need to specify.

It is selector of rathrola.component as shown below,

  1. import {  
  2.     Component  
  3. } from '@angular/core';  
  4. import {  
  5.     RathrolaComponent  
  6. } from './rathrola.component'  
  7. @Component({  
  8.     selector: 'my-app',  
  9.     template: `<h1>Hello World From Rathrola Prem Kumar</h1>  
  10. <h4>Header 4 from App component</h4>  
  11. <my-tuts></my-tuts>`,  
  12.     styles: [`h4{  
  13. color:blue;  
  14. }`],  
  15.     directives: [RathrolaComponent]  
  16. })  
  17. export class AppComponent {} // This is just a sample script. Paste your real code (javascript or HTML) here.  
  18. if ('this_is' == /an_example/) {  
  19.     of_beautifier();  
  20. else {  
  21.     var a = b ? (c % d) : e[f];  
  22. }  
Now, we need a way to inform our app component about rathrola.component that we do by using another configuration called directives.

Directives is going to have an array, it is just a name of the component as below

directives: [RathrolaComponent]

Note: we need to import rathrola.component before we create directive as shown above.

Run the application and see the output as below.


This is how we create a new component and then use it in other component using directives.

In our next article, we shall see how to style html code for a component.

Thanks for reading my article, and stay tuned.

Similar Articles