我在 Angular 2 项目中为我的 ag-grid 声明了一些 editorFrameWork 组件。但是,当我在编辑器组件的构造函数中注入任何服务时,TypeScript 编译器在加载网页时会引发以下错误:
Uncaught Error: Can't resolve all parameters for MyOtherEditorFrameworkComponent: (?)
at CompileMetadataResolver.getDependenciesMetadata (http://<rooturl here>/main.bundle.js:39398:19)
at CompileMetadataResolver.getTypeMetadata (http://<rooturl here>/main.bundle.js:39299:26)
at CompileMetadataResolver.getDirectiveMetadata (http://<rooturl here>/main.bundle.js:39074:28)
at http://<rooturl here>/main.bundle.js:39167:49
at Array.forEach (native)
at CompileMetadataResolver.getNgModuleMetadata (http://<rooturl here>/main.bundle.js:39161:49)
at RuntimeCompiler._compileComponents (http://<rooturl here>/main.bundle.js:57492:47)
at RuntimeCompiler._compileModuleAndComponents (http://<rooturl here>/main.bundle.js:57430:37)
at RuntimeCompiler.compileModuleAsync (http://<rooturl here>/main.bundle.js:57421:21)
at PlatformRef_._bootstrapModuleWithZone (http://<rooturl here>/main.bundle.js:41293:25)
我已经解决了另一个需要注入服务的编辑器组件的问题,方法是为组件声明一个模块并将该模块包含在 app.module 中。但是,这似乎不适用于第二个编辑器组件,如果我在 app.module 中添加下面类似定义的第二个编辑器组件模块以及第一个编辑器组件模块。
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { MyOtherEditorFrameworkComponent} from './my-other-editor-framework.component';
@NgModule({
imports: [
CommonModule
],
declarations: [MyOtherEditorFrameworkComponent]
})
export class MyOtherEditorFrameworkModule{ }
src/app/components/.../mySecondEditorFrameworkComponent/my-second-editor-framework-component.ts:
import { Component, OnInit, ViewChild, ViewContainerRef } from '@angular/core';
import { AgEditorComponent } from 'ag-grid-ng2/main';
import { MyInjectedService } from '../../../services/myinjectedservice/my-injected.service';
@Component({
selector: 'app-name-editor',
templateUrl: './name-editor.component.html',
styleUrls: ['./name-editor.component.sass']
})
export class mySecondEditorFrameworkComponent implements OnInit, AgEditorComponent {
@ViewChild('mySecondEditor', { read: ViewContainerRef }) mySecondEditor;
constructor(private myInjectedService : MyInjectedService ) { }
...}
src/app/app.module:
@NgModule({
imports: [
AgGridModule.withNg2ComponentSupport(),
MyOtherEditorFrameworkModule,
//MySecondEditorFrameworkModule, //<-- this throws the error
...],
...
providers: [ myInjectedService]
})