Posted onEdited onInangular Symbols count in article: 544Reading time ≈2 mins.
Introduction
In this article, we will learn how to dynamically load a component in Angular, In addition to load a component in html template directly, you can also load a component dynamically with NgComponentOutlet or ViewContainerRef
NgComponentOutlet
NgComponentOutlet is a directive that provide a declarative way for dynamic component creation, it requires a component type as input and create an instance of the component in the view.
1
*ngComponentOutlet="componentType"
Here are the code snippets to dynamically load a component with NgComponentOutlet:
// We return the component type by condition. getComponent() { returnthis.showProduct? ProductComponent: CustomerComponent; } }
Question: What’s the difference between *ngComponentOutlet and *ngIf* in this case? *ngIf can also load the component by condition.
ViewContainerRef
ViewContainerRef is a class that represents a container where one or more views can be attached to a component. It provides methods to create components and attach them to the container.
Explanation: The above code decides which component should be loaded when the user clicks the button, based on the showProduct variable. After running the program, press F12 to open the developer tools, and observe the Network tab. You can see that after clicking the Lazy load component button, the app initiates a request to dynamically load the Product component.
The component was bundled separately with name chunk-VFVWUJZL.js to support lazy load, if you want to see the real name of the chunk file, you can add namedChunk to your angular.json file.
In this way the chunk file will got a name like product.component-RGAR2EGQ.js
Finally, the getComponent method can be optimized to avoid duplicate code, but it seems that the readability is not very good after optimization. What do you think?
Posted onEdited onInangular Symbols count in article: 166Reading time ≈1 mins.
Introduction
In this post, we’ll discuss how to upgrade Angular to the latest version.
Steps
Suppose the current Angular version is 16.0.0, and the latest version is 17.0.0. Here are the steps to upgrade Angular to the latest version:
Update Node.js
First, make sure you have the latest version of Node.js installed on your machine. You can download the latest version from the official Node.js website.
Update Angular core/cli
Navigate to your Angular project root directory and run the following command to update Angular CLI to the latest version:
1
ng update @angular/core@16 @angular/cli@16
Update Angular Material/CDK
If your project use Angular Material or Angular CDK, you can update them to the latest version by running the following command:
1
ng update @angular/material@16 @angular/cdk@16
Update 3rd-party libraries
If your project uses any 3rd-party libraries, make sure to update them to the latest compatible version with Angular 17.
Posted onEdited onInangular Symbols count in article: 433Reading time ≈2 mins.
Introduction
In this article, we will learn how to use ChangeDetectionStrategy.OnPush in Angular to improve the performance of your application. By default, Angular uses ChangeDetectionStrategy.Default, which means that the change detection runs every time an event is triggered in the application. This can be inefficient if your application has a lot of components that don’t need to be updated every time an event is triggered. By using ChangeDetectionStrategy.OnPush, you can tell Angular to only run change detection on a component when its input properties change. with onPush strategy, Angular will only trigger change detection in the following cases:
When component’s input property changes
When an event is triggered in the component
When you manually trigger change detection
注意:这里好像还有一条是async pipe,但是我找不到文档了,以后找到再补充吧。
Example
Take a look at the product component below, it has an input property name, and an event handler which changes the price of the product. We have set the changeDetection property to ChangeDetectionStrategy.OnPush in the component’s metadata.
When parent component change the input property name, Angular will trigger change detection in the product component. and console will output change detection triggered...
When user click button to change the price, Angular will trigger change detection in the product component. and console will output change detection triggered...
setTimeout function will not trigger change detection in the product component.
Posted onEdited onInangular Symbols count in article: 198Reading time ≈1 mins.
Introduction
The angular.json file under the root of an Angular workspace provides workspace-wide and project-specific configuration defaults. It is used by the Angular CLI to build, test, and run Angular projects.
Path values given in the configuration are relative to the root of workspace directory.
Schema
You can find the schema file of angular.json from here: ./node_modules/@angular/cli/lib/config/schema.json
Source map is useful for debugging, it will generate a source map file for each JavaScript file. For example, main.xxx.js will have a main.xxx.js.map file when source map was on.
This option is useful when you want to debug the production build, with above option, it will not minify the scripts, but styles and fonts are still minified.
/* To learn more about this file see: https://angular.io/config/tsconfig. */ { "extends":"./tsconfig.json", "compilerOptions":{ "outDir":"./out-tsc/spec", "types":[ "jasmine" ] }, "include":[ "src/**/*.spec.ts", "src/**/*.d.ts" ] }
如果你尝试将src/**/*.spec.ts从include选项中删除,再运行npm run test,将会遇到如下错误。
1
An unhandled exception occurred: error TS18003: No inputs were found in config file '.../my-app/tsconfig.spec.json'. Specified 'include' paths were '["src/**/*.d.ts"]' and 'exclude' paths were '["./out-tsc/spec"]'
function parseJSON<T>(json: string): T { returnJSON.parse(json) as T; } interfaceUser { name: string; age: number; } const user = parseJSON<User>('{"name":"Philip","age":18}');