NgModules不使用NgUpgrade

时间:2016-09-01 14:18:25

标签: angular ng-upgrade

Angular 1& 2(RC5)应用程序使用ng-upgrade以混合模式运行。 尝试使用不起作用的@NgModule,我仍然需要手动添加提供程序:

 upgradeAdapter.addProvider(REACTIVE_FORM_PROVIDERS);

这阻止我从RC5-> RC6升级。 要使用外部组件,我仍然需要将它们添加到指令:数组中,用于它们正在使用的特定组件。

import { AlertComponent } from 'ng2-bootstrap/ng2-bootstrap';

@Component({
    selector: 'sb-staff-dashboard',
    templateUrl: 'staff-dashboard.component.html',
    styleUrls: ['staff-dashboard.component.scss'],
    directives: [ AlertComponent ]
})

这是我的相关引导代码。

upgradeAdapter.addProvider(COMMON_PROVIDERS);
upgradeAdapter.addProvider(HTTP_PROVIDERS);
upgradeAdapter.addProvider(DragulaService);
upgradeAdapter.addProvider(REACTIVE_FORM_PROVIDERS);
upgradeAdapter.bootstrap(document.documentElement, ['common', CommonModule], { strictDi: true });

upgradeAdapter.ts

import {UpgradeAdapter} from '@angular/upgrade';

export const upgradeAdapter = new UpgradeAdapter();

common.module.ts

import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from "@angular/forms";
import { HttpModule } from '@angular/http';
import { BrowserModule } from '@angular/platform-browser';
import { Ng2BootstrapModule } from 'ng2-bootstrap/ng2-bootstrap';
import { Dragula, DragulaService } from 'ng2-dragula/ng2-dragula';

@NgModule({
    imports: [
        BrowserModule,
        ReactiveFormsModule,
        Ng2BootstrapModule,
        HttpModule
    ],
    declarations: [
        COMMON_COMPONENTS,
        Dragula
    ],
    providers: [
        COMMON_PROVIDERS,
        DragulaService
    ]
})
export class CommonModule { }

1 个答案:

答案 0 :(得分:0)

您必须使用所需的模块创建UpgradeAdapter

例如:

import { forwardRef } from '@angular/core';
import { CommonModule } from '@angular/common';
import { UpgradeAdapter } from '@angular/upgrade';

export const upgradeAdapter = new UpgradeAdapter(forwardRef(() => CommonModule));

此外,您不必导入COMMON_COMPONENTSCOMMON_PROVIDERS,而是必须使用:

import { CommonModule } from '@angular/common';

@NgModule({imports: [CommonModule, ...]})