Create Component Angular 6

In this post we will learn how to Create Component Angular 6. Most work you do with Angular relates to components. Basically an Angular 6 application is a tree of components with a root component. A component controls a part of the web application screen. It consists of JavaScript (or TypeScript) code, HTML code and CSS. If you are familiar with the MVC (Model-View-Controller) architecture or design pattern, each component actually uses the same architecture: the component’s code represents the controller and the HTML&CSS code represents the view.

1. Install Bootstrap 4
In this post we will integrate Bootstrap 4 in a Angular 6 project generated using Angular CLI. Now install bootstrap 4 using the following command.

Now, add the following line inside src>>styles.css file.

Now the bootstrap 4 classes is already to use.

2. Create components.

Create the components folder inside app folder.
create component angular 6 folder

Here, we create three components (create,edit,index) by using the following command.

All the components are created inside components folder. The app.module.ts file will be automatically updated also. See detail in the console.
create component angular 6

3. Configure the routing
The Angular Router enables navigation from one view to the next as users perform application tasks, eg:

  • Enter a URL in the address bar and the browser navigates to a corresponding page.
  • Click links on the page and the browser navigates to a new page.
  • Click the browser’s back and forward buttons and the browser navigates backward and forward through the history of pages you’ve seen.
  • In this our application, we create one array called routes and then add the object that contains route path and route component inside app.module.ts file.

    Now, register these routes.

    Our app.module.ts looks like

    We can include the inside an app.component.html file. Router-outlet acts as a placeholder that marks the spot in the template where the router should display the components for that outlet.

    Let’s see the components based on routes as browsing http://localhost:4200/index then you can see “index works!” in the screen.

    That’s all about the post Create Component Angular 6.

    Introduction to components

    Download the complete source code, click link below

    ComponentAngular6App.rar (180 downloads)

    Leave a Comment


    Please share it if you found this useful
    Hide Buttons