2016-08-12 99 views
5

因爲我有角2 RC4以下main.ts文件:角2 RC4到RC5引導

import { bootstrap } from '@angular/platform-browser-dynamic'; 
import { enableProdMode,provide } from '@angular/core'; 
import {disableDeprecatedForms, provideForms} from '@angular/forms'; 
import {RequestOptions,HTTP_PROVIDERS} from '@angular/http'; 
import { AppComponent, environment } from './app/'; 
import { appRouterProviders } from './app/app.routes'; 
import {CustomRequestOptions} from "./app/custom-request-options"; 
import { Title } from '@angular/platform-browser'; 

if (environment.production) { 
    enableProdMode(); 
} 

bootstrap(AppComponent, [ 
    appRouterProviders, 
    disableDeprecatedForms(), 
    provideForms(), 
    HTTP_PROVIDERS, 
    provide(RequestOptions, {useClass: CustomRequestOptions}), 
    Title 
]) 
.catch(err => console.error(err)); 

現在..我需要它作爲一個模塊。所以我創建app.module.ts文件

import { NgModule }  from '@angular/core'; 
import { BrowserModule } from '@angular/platform-browser'; 
import { AppComponent } from './app.component'; 

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

如何導入我的所有供應商和declrations從以下線路:

bootstrap(AppComponent, [ 
    appRouterProviders, 
    disableDeprecatedForms(), 
    provideForms(), 
    HTTP_PROVIDERS, 
    provide(RequestOptions, {useClass: CustomRequestOptions}), 
    Title 
]) 

到模塊中的工作嗎?

回答

7

隨着RC5,你需要引導這樣:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic'; 
import { AppModule } from './app/app.module'; 

platformBrowserDynamic().bootstrapModule(AppModule); 

只需將您的供應商到模塊的那些(providers屬性):

@NgModule({ 
    (...) 
    providers: [ 
    provide(RequestOptions, {useClass: CustomRequestOptions}), 
    Title 
    ] 
}) 

對於形式,HTTP,路由,只需包含來自Angular2的相應模塊:

@NgModule({ 
    (...) 
    imports: [ 
    BrowserModule, 
    RouterModule.forRoot(config), 
    FormsModule, 
    HttpModule, // provides HTTP_PROVIDERS 
    ] 
}) 

查看遷徙有關詳細信息,ñDOC:

+0

您好亨利,可惜你上面分享的鏈接斷開。我有個問題。 ''platformBrowserDynamic()。bootstrapModule()'return'Promise'?在Angular2加載'AppModule'之後,我們可以做一些特定於App的引導工作嗎? – sanbhat