


Detailed explanation of the use of Angular entry components and declarative components
This time I will bring you a detailed explanation of the use of Angular entry components and declarative components. What are the precautions when using Angular entry components and declarative components? The following is a practical case, let's take a look.
Preface
Components are a very important part of Angular. The following article will introduce you to the Angular entry component. ) and the difference between declarative components. The difference between Angular's declarative components and entry components is reflected in the different loading methods of the two.
Declarative components are loaded through the selector declared by the component
The entry component (entry component) is loaded through the component Type dynamic loading
Declarative component
Declarative components will load the component through the selector declared by the component in the template.
Example
@Component({ selector: 'a-cmp', template: ` <p>这是A组件</p> ` }) export class AComponent {}
@Component({ selector: 'b-cmp', template: ` <p>在B组件里内嵌A组件:</p> <a-cmp></a-cmp> ` }) export class BComponent {}
In the BComponent template, use the selector<a-cmp></a-cmp>
statement to load AComponent.
Entry component (entry component)
The entry component loads the component through the specified component class.
is mainly divided into three categories:
Declared in
@Ng<a href="http://www.php.cn/code/8212.html" target="_blank">Module</a>.<a href="http://www.php.cn/wiki/1505.html" target="_blank">bootstrap</a>
Startup component, such as AppComponent.Others are loaded using component types through programming Dynamic component
Startup component
app.component.ts
@Component({ selector: 'app-root', templateUrl: './app.component.html', styleUrls: ['./app.component.scss'] }) export class AppComponent{}
app.module.ts
@NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, BrowserAnimationsModule, AppRoutingModule ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
in bootstrap The declared AppComponent is the startup component. Although we will use the component's selector<app-root></app-root>
position in index.html, Angular does not load the AppComponent based on this selector. This is where it can easily be misunderstood. Because index.html does not belong to any component template, Angular needs to load the component programmatically using the AppComponent type declared in bootstrap instead of using a selector.
Since Angular dynamically loads AppComponent, all AppComponent is an entry component.
Components referenced by routing configuration
@Component({ selector: 'app-nav', template: ` <nav> <a routerLink="/home" routerLinkActive #rla="routerLinkActive" selected="#rla.isActive">首页</a> <a routerLink="/users" routerLinkActive #rla="routerLinkActive" selected="#rla.isActive">用户</a> </nav> <router-outlet></router-outlet> ` }) export class NavComponent {}
We need to configure a route
const routes: Routes = [ { path: "home", component: HomeComponent }, { path: "user", component: UserComponent } ]; @NgModule({ imports: [RouterModule.forRoot(routes)], exports: [RouterModule] }) export class AppRoutingModule { }
Angular loads the component class specified by the route according to the configured route component instead of loading through the component's selector.
Configuring entry components
In Angular, the library components need to be configured in @NgModule.entryComponents
.
Since the components referenced in the startup component and routing configuration are both entry components, Angular will automatically add these two components to @NgModule.entryComponents
during compilation. For dynamic components written by ourselves, we need to manually add them to @NgModule.entryComponents
.
@NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, BrowserAnimationsModule, AppRoutingModule ], providers: [], entryComponents:[DialogComponent], declarations:[] bootstrap: [AppComponent] }) export class AppModule { }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Using CSS Modules elegant mode
##Detailed explanation of using vue-cli multi-module packaging
The above is the detailed content of Detailed explanation of the use of Angular entry components and declarative components. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The login method for the 192.168.0.1 portal is: enter http://192.168.0.1 in the address bar of the browser. The login method for analyzing the 1168.0.1 entrance is: enter http://192.168.0.1 in the address bar of the browser. After entering the account password, you can enter a page and set or change relevant important network parameters. This URL is usually the login page of routers from brands such as Tenda, Cisco, D-X, and Links. If you cannot enter the management interface after entering it, it means the address is wrong. Supplement: What does 192.168.0.1 mean? 1168.0.1 is a Class C private IP address. To put it simply, 192.168.0.1 is an IP address.

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

This article will give you an in-depth understanding of Angular's state manager NgRx and introduce how to use NgRx. I hope it will be helpful to you!

With the rapid development of the Internet, front-end development technology is also constantly improving and iterating. PHP and Angular are two technologies widely used in front-end development. PHP is a server-side scripting language that can handle tasks such as processing forms, generating dynamic pages, and managing access permissions. Angular is a JavaScript framework that can be used to develop single-page applications and build componentized web applications. This article will introduce how to use PHP and Angular for front-end development, and how to combine them

Do you know Angular Universal? It can help the website provide better SEO support!

This article will share with you an Angular practical experience and learn how to quickly develop a backend system using angualr combined with ng-zorro. I hope it will be helpful to everyone!

How to use monaco-editor in angular? The following article records the use of monaco-editor in angular that was used in a recent business. I hope it will be helpful to everyone!

This article will take you through the independent components in Angular, how to create an independent component in Angular, and how to import existing modules into the independent component. I hope it will be helpful to you!
