This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24383|Reply: 4

[Angular] Angular 9 Series (1) Dynamically Loaded Components

[Copy link]
Posted on 5/29/2020 4:40:00 PM | | | |
Angular 9 Series (1) Dynamically Loaded Components
https://www.itsvse.com/thread-9238-1-1.html

Angular 9 series (2) dynamic component transmission parameters
https://www.itsvse.com/thread-9245-1-1.html

Angular 9 series (3) subscribes to dynamic component events
https://www.itsvse.com/thread-9246-1-1.html

Angular 9 Series (IV) Custom Pipe pipelines
https://www.itsvse.com/thread-9248-1-1.html

Angular 9 Series (V) formats the amount currency format
https://www.itsvse.com/thread-9249-1-1.html



The template of a component will not always be fixed. Your app might need to load some new components during runtime.

Usage Scenarios:

1. Page loading loads different components according to different parameters, showing different effects
2. The page is composed of multiple elements, not all of them need to be displayed at the beginning, and specific information will be displayed after the user clicks, such as: tab tabs, dynamic pop-up modal boxes, etc.

The usage scenario of this article is 2, just imagine,If you don't use dynamic loading, when you open the main interface, all the hidden components of the page will be loaded, and all components call their corresponding interface services, often, we don't need to see all the information, which is a great waste of resources, and may also cause the main interface to load slowly, affecting the user experience

There are two ways to dynamically load components in ng:

Load already declared components: Use ComponentFactoryResolver to render an instance of a component to another component view;
Create and load components dynamically: Use ComponentFactory and Compiler to create and render components
According to our needs, the individual components are developed in advance and need to be displayed on the same component. So the first way meets our requirements.

To dynamically load components using ComponentFactoryResolver, you need to understand the following concepts:

  • ViewChild: Property decorator, through which you can get the corresponding elements on the view;
  • ViewContainerRef: A view container on which components can be created and deleted;
  • ComponentFactoryResolver: A component parser that can render one component on a view of another.

First, let's look at the renderings, click the modal1 button to dynamically load the modal1 component, and click the modal2 button to dynamically load the modal2 component.



First, we create an angular project.

Create 4 components, namely index, modal1, modal2, and modal3, of which modal1 and modal2 are our dynamically loaded components.

index component

html:

ts:

modal1 component

html:

ts:


modal2 component

html:


ts:

modal3 component

html:


ts:


We printed their names on the constructor for 3 modals.

modal1 and modal2 are both dynamically loaded, only when the button is clicked to load, the execution constructor will be triggered, modal3 does not use dynamic loading, after the parent interface is loaded, the modal3 interface constructor will be executed immediately.

In this way, you can see the advantages of dynamic loading, and it will not load if you don't use it, similar to lazy loading in C#.

Source code download:

Tourists, if you want to see the hidden content of this post, pleaseReply


Resources:The hyperlink login is visible.






Previous:CSS setting height: 100% invalid solution
Next:Azure DevOps 2020 (1) First Introduction to Microsoft Azure DevOps (TFS)
 Landlord| Posted on 7/24/2020 5:52:58 PM |
Angular 9 Series (1) Dynamically Loaded Components
https://www.itsvse.com/thread-9238-1-1.html

Angular 9 series (2) dynamic component transmission parameters
https://www.itsvse.com/thread-9245-1-1.html

Angular 9 series (3) subscribes to dynamic component events
https://www.itsvse.com/thread-9246-1-1.html

Angular 9 Series (IV) Custom Pipe pipelines
https://www.itsvse.com/thread-9248-1-1.html

Angular 9 Series (V) formats the amount currency format
https://www.itsvse.com/thread-9249-1-1.html

Angular 9 series (VI) calls native JS variables and methods
https://www.itsvse.com/thread-9254-1-1.html

Angular 9 series (VII) CSS-style scope
https://www.itsvse.com/thread-9264-1-1.html

Angular 9 series (VIII) updates the value of a child component by triggering an event via set
https://www.itsvse.com/thread-9267-1-1.html

Angular 9 Series (nine) #id selector applied on the page
https://www.itsvse.com/thread-9278-1-1.html

Angular 9 series (10) embeds vconsole and eruda developer debugging panels
https://www.itsvse.com/thread-9286-1-1.html

Angular 9 Series (XI) 5 Ways to Define Styles
https://www.itsvse.com/thread-9305-1-1.html


Angular 9 series (XII) generates dynamic tokens based on OTPs
https://www.itsvse.com/thread-9325-1-1.html
 Landlord| Posted on 2/2/2021 1:56:30 PM |
Angular 10 series (thirteen) introduces Baidu echarts charts
https://www.itsvse.com/thread-9347-1-1.html

Angular 11 series (XIV) production environment for debugging
https://www.itsvse.com/thread-9348-1-1.html

Angular 11 Series (XV) acquires component height and dynamically adjusts height
https://www.itsvse.com/thread-9364-1-1.html

Angular 11 series (16) page parsing HTML source code
https://www.itsvse.com/thread-9376-1-1.html
Posted on 10/26/2022 5:27:19 PM |
Learn!!!!!!!!!!!!!!
 Landlord| Posted on 4/30/2025 2:38:35 PM |
ComponentFactoryResolverDeprecated

The amendments are as follows:
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com