W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
Angular 應(yīng)用至少需要一個充當根模塊使用的模塊。 如果你要把某些特性添加到應(yīng)用中,可以通過添加模塊來實現(xiàn)。 下列是一些常用的 Angular 模塊,其中帶有一些其內(nèi)容物的例子:
NgModule | 導(dǎo)入自 | 為何使用 |
---|---|---|
BrowserModule | @angular/platform-browser | 當你想要在瀏覽器中運行應(yīng)用時 |
CommonModule | @angular/common | 當你想要使用 NgIf 和 NgFor 時 |
FormsModule | @angular/forms | 當要構(gòu)建模板驅(qū)動表單時(它包含 NgModel ) |
ReactiveFormsModule | @angular/forms | 當要構(gòu)建響應(yīng)式表單時 |
RouterModule | @angular/router | 要使用路由功能,并且你要用到 RouterLink,.forRoot() 和 .forChild() 時 |
HttpClientModule | @angular/common/http | 當你要和服務(wù)器對話時 |
當你使用這些 Angular 模塊時,在 AppModule
(或適當?shù)奶匦阅K)中導(dǎo)入它們,并把它們列在當前 @NgModule
的 imports
數(shù)組中。比如,在 Angular CLI 生成的基本應(yīng)用中,BrowserModule
會在 "app.module.ts" 中 AppModule
的頂部最先導(dǎo)入。
/* import modules so that AppModule can access them */
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [ /* add modules here so Angular knows to use them */
BrowserModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
文件頂部的這些導(dǎo)入是 JavaScript 的導(dǎo)入語句,而 @NgModule
中的 imports
數(shù)組則是 Angular 特有的。
BrowserModule
導(dǎo)入了 CommonModule
,它貢獻了很多通用的指令,比如 ngIf
和 ngFor
。 另外,BrowserModule
重新導(dǎo)出了 CommonModule
,以便它所有的指令在任何導(dǎo)入了 BrowserModule
的模塊中都可以使用。
對于運行在瀏覽器中的應(yīng)用來說,都必須在根模塊中 AppModule
導(dǎo)入 BrowserModule
,因為它提供了啟動和運行瀏覽器應(yīng)用時某些必須的服務(wù)。BrowserModule
的提供者是面向整個應(yīng)用的,所以它只能在根模塊中使用,而不是特性模塊。 特性模塊只需要 CommonModule
中的常用指令,它們不需要重新安裝所有全應(yīng)用級的服務(wù)。
如果你把 BrowserModule
導(dǎo)入了惰性加載的特性模塊中,Angular 就會返回一個錯誤,并告訴你要改用 CommonModule
。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: