2017-08-13 151 views
2

典型根模塊看起來像這樣角4根模塊「進口」和在角度4「進口」

import { BrowserModule } from '@angular/platform-browser'; 
import { NgModule } from '@angular/core'; 
import { FormsModule } from '@angular/forms'; 
import { HttpModule } from '@angular/http'; 

import { AppComponent } from './app.component'; 

@NgModule({ 
    declarations: [ 
    AppComponent 
    ], 
    imports: [ 
    BrowserModule, 
    FormsModule, 
    HttpModule 
    ], 
    providers: [], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { } 

在本文檔中,提到「在單根模塊的上面的例子,該應用程序模塊需要該瀏覽器模塊中的材料。要訪問該材料,請將其添加到@NgModule元數據導入中,如下所示。「 imports: [ BrowserModule ],

我的問題是:

什麼是「應用模塊需要的材料從該瀏覽器模塊中的」是什麼意思?

這個進口import { BrowserModule } from '@angular/platform-browser';和這個imports: [ BrowserModule ],有什麼區別?

有人能幫我理解在根模塊中使用imports的目的嗎?

+0

https://angular.io/guide/ngmodule-faq –

+0

[angular2 - '導入'和'導出'如何在Angular2模塊中工作?](https://stackoverflow.com/questions/41281780/angular2-how-do-imports-and-exports-work-in-angular2-modules) – Milad

+0

AppModule =應用程序模塊.... BrowserModule嵌入在此中。頂部的導入用於定位它的位置 – JGFMK

回答

1

「應用程序模塊需要該 瀏覽器模塊中的材料」是什麼意思?

這可能意味着幾件事。首先,這可能意味着browser模塊定義了一些需要提供的提供者是全局注入器。它也可能意味着在根模塊中定義的組件將使用來自browser模塊的一些可申報類型。其實,如果你看看BrowserModule定義你會認爲這是兩個:

@NgModule({ 

    // global providers 
    providers: [ 
    BROWSER_SANITIZATION_PROVIDERS, 
    {provide: ErrorHandler, useFactory: errorHandler, deps: []}, 
    {provide: EVENT_MANAGER_PLUGINS, useClass: DomEventsPlugin, multi: true}, 
    ... 
    ], 

    // declarable types - components/directives/pipes 
    // declared in common and application modules 
    exports: [CommonModule, ApplicationModule] 
}) 
export class BrowserModule { 

我建議你閱讀Avoiding common confusions with modules in Angular

是什麼這個進口import { BrowserModule } from '@angular/platform-browser';而這一次imports: [ BrowserModule ]

之間的區別不同的是,前者導入ESM模塊,後者導入Angular模塊。 ESM/Typescript modules與Angular模塊無關。