我可以轻松地将angular2提供者添加到UpgradeAdapter:
import { HTTP_PROVIDERS } from 'angular2/http';
upgradeAdapter.addProvider(HTTP_PROVIDERS);
我可以很容易地升级ng1指令(组件):
const DirectiveNg1 = upgradeAdapter.upgradeNg1Component('DirectiveNg1');
但我在很多地方都需要DirectiveNg1
,并希望在angular2组件中使用它们。是否有可能获得参考?
目前我在angular2组件中指定了以下内容并且它可以正常工作,但我想在main.js文件中只有upgradeNg1Component('DirectiveNg1')
一次。
const DirectiveNg1 = upgradeAdapter.upgradeNg1Component('DirectiveNg1');
// how can I replace the line above, and get a reference from the UpgradeAdapter
@Component({
directives: [DirectiveNg1],
...
})
答案 0 :(得分:0)
我知道这篇文章有点老了,但这花了我很长时间的互联网搜索工作。我认为这方面的文档非常好。
这种最简单的方法是,你只需拥有一个模块。您可以按如下方式定义它。
import { NgModule, forwardRef } from '@angular/core';
import { UpgradeAdapter } from '@angular/upgrade';
export const upgradeAdapter = new UpgradeAdapter(forwardRef(() => AppModule ));
const DirectiveNg1 = upgradeAdapter.upgradeNg1Component('DirectiveNg1');
@NgModule({
imports: [BrowserModule, HttpModule],
providers: [],
declarations: [DirectiveNg1],
exports: []
})
export class AppModule { }
现在DirectiveNg1可用于angular2组件。