In this article, we are going to talk about components in Angular.
As we know, a component is the basic building block of Angular, which means that an Angular application is a collection of components, and one component is responsible for handling one view or part of the view.
A component is nothing but a simple typescript class which contains the following things,
- Typescript Class
- HTML template/Template URL
- @Component decorator with metadata.
So in this article, we will learn about the component. We will create one component step by step and display a message in the browser. Please note that I have downloaded the required files to run the Angular application from the Angular official website -- you can download them from my GitHub repository using below link.
https://github.com/mahesh353/AngularProjectFiles
Now, follow the below steps.
Step 1
Create one typescript file and name it as app.component.ts and paste the below code
- import { Component } from "@angular/core";
- //@Component is a decorator which is present in @angular/core package
- // so to use that I have import that package at the top.
- // in @Component decorator I have used two metadata tags
- // selector - this is use to give the name to component so that we can use that name as directive
- // template - this is used to specify the html template
- // templateUrl - we can use this if we are referencing the html written in other file
- @Component({
- selector: 'my-app',
- template: '<h1>Welcome to C# Corner</h1>'
- })
- export class AppComponent {
- }
In the above code, I have created one class AppComponent using the class keyword like other object-oriented languages, And I have used the export keyword in front of the class declaration which means that other modules can easily use this component by importing it. See the below code snippet.
- export class AppComponent {
- }
To make this class an Angular component we need to decorate the above class using the @Component decorator which is present in the @angular/core package. That’s why we need to import @angular/core as well using the below code snippet.

Tridip BhattacharjeePosted Apr 9, 2018, 6:10 AM
Good article. i have a question that what is the meaning of bootstrap: [AppComponent] ? if we have multiple component then can we bootstrap component by comma ? share some knowledge. thanks
Satish Kumar VadlavalliPosted Apr 6, 2018, 8:11 PM
Clear explanation.....