详解 Angular ç¬ç«ç»ä»¶ | AIçæåç¿»è¯
Question: å¦ä½çè§£ Angular ä¸ç standalone componentsï¼
Answer:
Standalone components æ¯ç°ä»£ Angular 䏿éè¦çååä¹ä¸ï¼å¨ Angular 14 ä½ä¸ºå¼åè é¢è§çå¼å ¥ï¼å¨ Angular 15 è¾¾å°ç产å¯ç¨ç¶æï¼å¹¶ä» Angular 16+ å¼å§æä¸ºæ¨èçé»è®¤ç»ä»¶ç¼åæ¹å¼ï¼ã
âStandaloneâ çå®é å«ä¹
ä¸ä¸ª standalone component æ¯æï¼
- ä¸å±äºä»»ä½ NgModule
- ç´æ¥å£°æå ¶èªèº«ç dependenciesï¼å ¶ä» componentsãdirectivesãpipesãservicesâ¦ï¼
- å¯ä»¥ç´æ¥ç¨äº routingãå¨å¦ä¸ä¸ª component ç template ä¸ä½¿ç¨ï¼æä½ä¸ºå ¥å£ç¹
ç®èè¨ä¹ï¼å®å¨å¤§å¤æ°æ åµä¸æ¶é¤äºå¯¹ç»å ¸ NgModule æ ·æ¿ä»£ç çéæ±ã
ç»å ¸ï¼åºäº NgModuleï¼çæ¹å¼ â standalone ä¹å
// classic.component.ts
@Component({
selector: 'app-classic',
template: `<h1>Hello</h1>`,
standalone: false // â é»è®¤å¼ / ä¸éè¦
})
export class ClassicComponent {}
// ä½ å¿
é¡»å¨ä¸ä¸ª module ä¸å£°æå®
@NgModule({
declarations: [ClassicComponent],
imports: [CommonModule, FormsModule],
exports: [ClassicComponent]
})
export class SomeModule {}
Standalone æ¹å¼ â ç°ä»£ Angular (2023â2026 飿 ¼)
// modern.component.ts
import { Component } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { MatButtonModule } from '@angular/material/button';
import { HighlightDirective } from './highlight.directive';
@Component({
selector: 'app-modern',
standalone: true, // â è¿æ¯å
³é®è¡
imports: [ // â ååºæ¤ç»ä»¶éè¦çææå
容
CommonModule,
FormsModule,
MatButtonModule,
HighlightDirective, // â å¯ä»¥å¯¼å
¥å
¶ä» standalone components/directives
// 妿éè¦ï¼ä¹å¯ä»¥å¯¼å
¥æ´ä¸ª NgModulesï¼è¿æ¸¡æï¼
],
template: `
<h1>Hello from standalone!</h1>
<input type="text" [(ngModel)]="name">
<button mat-button>Click</button>
`
})
export class ModernComponent {
name = 'Angular';
}
å¦ä½ä½¿ç¨ Standalone Componentsï¼æ é NgModuleï¼
- å¨ router ä¸ï¼ç®åæå¸¸è§çç¨ä¾ï¼
const routes: Routes = [
{ path: 'home', component: HomeComponent }, // â standalone component
{ path: 'profile', component: ProfileComponent },
{ path: '', redirectTo: '/home', pathMatch: 'full' }
];
- å¨å¦ä¸ä¸ªç»ä»¶ç template å é¨
<!-- parent.component.html -->
<app-modern></app-modern>
<app-user-card [user]="currentUser"></app-user-card>
- ä½ä¸ºæ ¹ç»ä»¶ (bootstrapApplication)
// main.ts
import { bootstrapApplication } from '@angular/platform-browser';
import { provideRouter } from '@angular/router';
import { AppComponent } from './app.component';
import { routes } from './app.routes';
bootstrapApplication(AppComponent, {
providers: [
provideRouter(routes)
]
});
å¿«é对æ¯è¡¨ â NgModule vs Standalone
å¿æºæ¨¡å â åè¿æ ·çè§£ Standalone
ä¸ä¸ª standalone component å°±åä¸ä¸ªå°åãèªå å«çå¾®ååºç¨ï¼
- å®å£°æï¼âæéè¦è¿äºé¨åæè½å·¥ä½â
- Angular ç compiler æ£å¥½æå è¿äºé¨å
- ä¸åéè¦ä¸å¤®âæ¨¡åæ³¨å表â
è¿é常æ¥è¿ ReactãVueãSvelte çç»ä»¶ç工使¹å¼ã
å®è·µä¸å¼å§çè§£çæä½³æ¹å¼ (2026)
- å建ä¸ä¸ªä½¿ç¨ standalone çæ°é¡¹ç®ï¼èª Angular 17 起为é»è®¤è®¾ç½®ï¼ï¼
ng new my-app --standalone
- å°ç»ä»¶çæä¸º standaloneï¼ç°å¨çé»è®¤è¡ä¸ºï¼ï¼
ng g component user-card # â èªå¨å建 standalone: true
- è§å¯çæç代ç â ä½ å ä¹åä¹çä¸å°
declarations: []æ NgModules äºã
è¿æ¯âæåâ standalone å«ä¹çæå¿«æ¹æ³ã
Standalone ä¸ä» ä» æ¯ä¸ç§è¯æ³ââ宿¯ Angular åæ´ç®åãæ´ç°ä»£çç»ä»¶æ¨¡åç转åï¼è¯¥æ¨¡åå»é¤äºå¤§é¨å模åä»ªå¼æï¼åæ¶ä¿çäºææå¼ºå¤§åè½ï¼dependency injectionãlazy-loading çï¼ã
References:
- https://angular.dev/guide/standalone-components
- https://blog.angular.dev/introducing-standalone-components-58a418173aa3
- https://blog.angular.dev/angular-v17-is-now-available-8c4c0d8d3169
- https://angular.love/standalone-components-in-angular
