Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Declaring What a Module Owns with the Declarations Array in Angular

Jul 23, 2020 • 6 Minute Read

Introduction

Every Angular module has a declarations array in which you can register Angular components, directives, and pipes. In this guide, you will learn how to register a component, directive, or pipe with a declarations array and what it means to register these artifacts.

The @Component decorator needs information from the @NgModule decorator, which declares the components to generate component definition. The selector of a component is determined through the module that declares the component and used during the compilation of the component's template. For these reasons, global information about components is needed so that the same selector for components does not hamper the compilation of other modules' components.

The Declarations Array

The declarations array is used to declare components, directives, and pipes into the module in which they belong. Every component, directive, and pipe gets to know about others through this declaration. Without this declaration, a component would not be able to use directives and pipes.

For example, say you have a component that renders a technical event on a website like pluralsight.com, and you want to render technical event names in upper case using a pipe called toUpper. This component will be able to use the pipe toUpper only if the pipe is declared in the declarations array. Similarly, a component will be able to use another component as its child component only if both the components are declared in the declarations array, and a component will be able to use a directive only when the directive is declared in the declarations array.

Getting Started

Prerequisites

To complete this guide, you must have the following:

  1. Node - 12.17.0
  2. Angular CLI - 9.0.2
  3. Visual Studio Code - 1.45.1

Creating an Angular Project

  1. Execute the following command to create an Angular project.

ng new declarations-demo

  1. When prompted to add routing to the application, press N.
  2. When prompted to choose a stylesheet format, press ENTER.

Opening the Generated Application in Visual Studio Code

  1. Click on the File menu in the top menu bar and select the menu option Open Folder.
  2. In the file dialog box, navigate to the folder where you generated the application.
  3. In this folder, you should see a folder named declarations-demo. Select this folder and click Open in the file dialog box.

Setting up the Project

The next step is to install Twitter Bootstrap and jQuery and refer them in the project so you can use some of the form styles to make the form look good.

  1. Go to command prompt and run following command:

cd declarations-demo

  1. Run the following command to install jquery:

npm i jquery

  1. Run the following command to install Bootstrap:

npm i bootstrap

  1. In Visual Studio Code, open the file angular.json.
  2. Add the following line in the styles array before src/style.css.

"node_modules/bootstrap/dist/css/bootstrap.min.css",

  1. Add the following lines in the scripts array:

"node_modules/jquery/dist/jquery.min.js", "node_modules/bootstrap/dist/js/bootstrap.min.js"

  1. In Visual Studio Code, open the file src > index.html.
  2. At line number 10 on the body element, add the following class:

class="container"

The final code should look like this:

      <body class="container">
  <app-root></app-root>
</body>
    

Now the project is set to develop the demo application.

Creating a Component

  1. In Visual Studio Code, expand the src folder.

  2. Right-click on the app folder and choose menu option New Folder.

  3. Name the new folder demo.

  4. Right-click on the demo folder and choose the menu option New File.

  5. Name the new file demo.component.ts.

  6. Copy the following code and paste it in the file demo.component.ts.

      import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html'
})
export class DemoComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}
    
  1. Right-click on the demo folder and choose the menu option New File.

  2. Name the new file demo.component.html.

  3. Copy the following code and paste it into the file demo.component.html.

      <h1>demo works!</h1>
    

Using the Component

In Visual Studio Code, open the file src > app > app.component.html, delete the existing content, and add the following code.

      <app-demo></app-demo>
    

This code ensures that the App component will try to load and process the demo component and render the resultant HTML inside its HTML template.

Running Angular Dev Server in Watch Mode

  1. Run the following command in the command prompt to start Angular Development Server in watch mode:

ng serve

  1. If prompted to share Angular CLI usage data, press the N key to not share it.

  2. Notice that you are getting the following error:

ERROR in src/app/app.component.html:1:1 - error NG8001: 'app-demo' is not a known element

This error means that Angular is not able to recognize the element as a component. So you will have to register this component in the declarations array of the App module so that Angular recognizes this element as a component.

Registering the Component in Declarations Array

  1. In Visual Studio Code, open the file src > app > app.module.ts.

  2. In the declarations of the @NgModule decorator, add the DemoComponent. The final code should look like this:

      import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { DemoComponent } from './demo/demo.component';

@NgModule({
  declarations: [
    AppComponent,
    DemoComponent
  ],
  imports: [
    BrowserModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }
    

Testing the Fix

  1. Notice that the compilation error is gone from the command prompt.

  2. Open the following URL in your web browser:

https://localhost:4200

  1. You should now see the component rendering fine in the browser.

Conclusion

Congratulations!! You have learned how to register a component in the declarations array of any module. For more information, please refer to NgModules.

Parul Saxena

Parul S.

Parul Saxena is a professional developer, instructor, technical writer and ex-IBM consultant. Her career spans more than 14 years. She has designed and delivered enterprise-grade middleware solutions using IBM Enterprise Service Bus, DataPower and WebSphere to high profile retail and banking enterprises across the world. She has designed and implemented DevOps processes using AWS DevOps for consulting engagements in IBM Global Services. In addition, she has also written support workbooks and designed e-learning courses for multiple SaaS products.

More about this author