角度2(rc6)で同じ基本コンポーネントを再利用する場合、基本コンポーネントを拡張するすべてのコンストラクタで同じ依存宣言を使用する必要があります。なぜなら、常に同じ依存関係宣言を注入しようとするからです(私はそれが基本コンポーネントを拡張する最後にインポートされたコンポーネントだと思います)。同じ基本コンポーネントを再利用する場合の角度2依存性注入
例えば、Iベース成分 "AnimalComponent" を有する:
@Component({
selector: '#animalComponent',
template: `I'm a animal component`
})
export class AnimalComponent
{
constructor(
@Inject('AnimalData') protected _data: any
) {}
}
を私が "CatComponent" 二つの成分にベースコンポーネントを拡張し、 "DogComponent"
@Component({
selector: '#catComponent',
template: `I'm a cat component`
})
export class CatComponent extends AnimalComponent
{
constructor(
@Inject('CatData') data: any
) {
super(
data
);
}
}
@Component({
selector: '#dogComponent',
template: `I'm a dog component`
})
export class DogComponent extends AnimalComponent
{
constructor(
@Inject('DogData') data: any
) {
super(
data
);
}
}
、私は両方をラップモジュール内のコンポーネントなので、実行時にロードすることができます。
@NgModule({
imports: [CommonModule],
declarations: [CatComponent],
exports: [CatComponent]
})
export class CatModule {}
@NgModule({
imports: [CommonModule],
declarations: [DogComponent],
exports: [DogComponent]
})
export class DogModule {}
最後に、t試みは「DogComponent」の依存性注入を解決するとき、彼は、このダミーの場合には、私の最後のコンポーネント
import {DogModule} from './dog.module';
import {CatModule} from './cat.module';
@Component({
selector: '#mainComponent',
template: `
I'm a main component
<template #catComponent></template>
<template #dogComponent></template>
`
})
export class MainComponent
{
@ViewChild('catComponent', {read: ViewContainerRef}) catView: ViewContainerRef;
@ViewChild('dogComponent', {read: ViewContainerRef}) dogView: ViewContainerRef;
constructor(
protected _injector: Injector,
protected _compiler: Compiler
) {
let catInjector = ReflectiveInjector.fromResolvedProviders(
ReflectiveInjector.resolve({provide: 'CatData', useValue: 'Some cat information'}),
this._injector
);
this._compiler.compileModuleAndAllComponentsAsync(CatModule).then(
moduleFactory => {
let compFactory = moduleFactory.componentFactories.find(tmpCompFactory => tmpCompFactory.componentType.name === 'CatComponent');
let componentRef = this.catView.createComponent(compFactory, 0, catInjector, []);
}
);
let dogInjector = ReflectiveInjector.fromResolvedProviders(
ReflectiveInjector.resolve({provide: 'DogData', useValue: 'Some dog information'}),
this._injector
);
this._compiler.compileModuleAndAllComponentsAsync(DogModule).then(
moduleFactory => {
let compFactory = moduleFactory.componentFactories.find(tmpCompFactory => tmpCompFactory.componentType.name === 'DogComponent');
let componentRef = this.dogView.createComponent(compFactory, 0, dogInjector, []);
}
);
}
}
に2つのコンポーネントは、コンポーネントの注射は、メッセージの聖霊降臨祭分割されます:「!CatDataなしのプロバイダを」しかし、同じ名前( "CatData"と "DogData"ではなく "AnyAnimalData")の両方のプロバイダの名前を変更すると、すべて正常に動作します。
他にも同じ問題がありますか?私は何か間違っているのですか?