6
ディレクティブがあり、複数のモジュールで使用したい。各モジュールでそれを宣言すると、エラーが発生します。したがって、私はそれを共有モジュールで宣言し、この共有モジュールを他のモジュールにインポートしようとしました。しかし、それは動作しませんでした。これは、コンポーネントのモジュールで正確に宣言されている場合にのみ機能します。ここでAngular2 - 同じモジュールで宣言されていないときにディレクティブが動作しない
は私SharedModule
です:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { GeneralDataService } from './general-data.service';
import {ModalDirective} from '../directives/modal.directive';
@NgModule({
imports: [
CommonModule
],
providers: [GeneralDataService],
declarations: [ModalDirective]
})
export class SharedModule { }
そして、私はModalDirective
を使用するモジュール:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyComponent } from './patient.component';
import {SharedModule} from '../shared/shared.module';
@NgModule({
imports: [
CommonModule,
SharedModule
],
providers: [ ],
declarations: [ MyComponent ]
})
export class MyModule { }
MyComponent
コンポーネントで:MyComponent
で
export class MyComponent implements OnInit, AfterViewInit {
@ViewChild(ModalDirective) modal: ModalDirective;
...
}
modal
ですたとえModalDirective
がMyModule
に宣言されていない場合は、(ngAfterViewInit
でも)です。誰でもこれを修正する方法を知っていますか?あなたのSharedModule
あなたがModalDirective
...
@NgModule{(
...
exports: [ModalDirective]
)}
export class SharedModule { ... }
をexport
する必要がで
の共有モジュールでdocsを参照してください。ありがとうございました! –
Thxフレドリック!私の指示には 'TemplateRef'の注入があります。しかし、 'NgModule.exports'リストにディレクティブを追加すると、' No Provider for TemplateRef! 'というエラーが出ます。何か案は? – cosmozhang
@cosmozhang - あなたのコードを見ることなく実際にはわかりませんが、このスレッドを見てください:https://stackoverflow.com/questions/35932074/angular2-no-provider-for-templateref-ngif-templateref –