Angular 2 RC4到RC5自举

时间:2016-08-12 13:44:30

标签: angular

自Angular 2 RC4以来,我有以下main.ts文件:

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 {}

如何从以下行导入所有提供者和摘录:

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

在模块中工作?

1 个答案:

答案 0 :(得分: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
  ]
})

有关详细信息,请参阅迁移文档: