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 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.
To complete this guide, you must have the following:
ng new declarations-demo
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.
cd declarations-demo
npm i jquery
npm i bootstrap
angular.json
.styles
array before src/style.css
."node_modules/bootstrap/dist/css/bootstrap.min.css",
scripts
array:"node_modules/jquery/dist/jquery.min.js",
"node_modules/bootstrap/dist/js/bootstrap.min.js"
src > index.html
.class="container"
The final code should look like this:
1<body class="container">
2 <app-root></app-root>
3</body>
Now the project is set to develop the demo application.
In Visual Studio Code, expand the src
folder.
Right-click on the app
folder and choose menu option New Folder.
Name the new folder demo
.
Right-click on the demo
folder and choose the menu option New File.
Name the new file demo.component.ts
.
demo.component.ts
.1import { Component, OnInit } from '@angular/core';
2
3@Component({
4 selector: 'app-demo',
5 templateUrl: './demo.component.html'
6})
7export class DemoComponent implements OnInit {
8
9 constructor() { }
10
11 ngOnInit(): void {
12 }
13
14}
Right-click on the demo
folder and choose the menu option New File.
Name the new file demo.component.html
.
demo.component.html
.1<h1>demo works!</h1>
In Visual Studio Code, open the file src > app > app.component.html
, delete the existing content, and add the following code.
1<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.
ng serve
If prompted to share Angular CLI usage data, press the N key to not share it.
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.
In Visual Studio Code, open the file src > app > app.module.ts
.
declarations
of the @NgModule
decorator, add the DemoComponent
. The final code should look like this:1import { BrowserModule } from '@angular/platform-browser';
2import { NgModule } from '@angular/core';
3
4import { AppComponent } from './app.component';
5import { DemoComponent } from './demo/demo.component';
6
7@NgModule({
8 declarations: [
9 AppComponent,
10 DemoComponent
11 ],
12 imports: [
13 BrowserModule
14 ],
15 providers: [],
16 bootstrap: [AppComponent]
17})
18export class AppModule { }
Notice that the compilation error is gone from the command prompt.
http://localhost:4200
Congratulations!! You have learned how to register a component in the declarations array of any module. For more information, please refer to NgModules.