在角度库的角度属性指令中注入第三方组件

时间:2018-09-26 22:08:03

标签: angularjs angularjs-directive angular-library

错误: NullInjectorError:没有ColumnComponent的提供程序!

我创建了一个角度库,然后在该库内创建了一个属性指令,主应用程序使用该指令将数据绑定到第三方组件的属性。

src / app / client.component.html

<kendo-grid-column field="id" fcidColumnBinding>
</kendo-grid-column>

这是我在图书馆方面的东西: projects / metadata-lib / src / lib / column-binding-directive.ts

@Directive({
  selector: '[fcidColumnBinding]'
})
export class ColumnBindingDirective {

  constructor(private metadataLibService: MetadataLibService, private gridColumn: ColumnComponent) { }
  public ngOnInit(): void {
    //Called after the constructor, initializing input properties, and the first call to ngOnChanges.
    //Add 'implements OnInit' to the class.
    this.metadataLibService.subscribe((result) => {
      this.gridColumn.title = this.metadataLibService.getUIHint(result, this.gridColumn.field, 'DisplayName');
      this.gridColumn.width = this.metadataLibService.getUIHint(result, this.gridColumn.field, 'columnWidth');
    })
    this.rebind();
  }
  public rebind(): void {
    this.metadataLibService.query();
  }
}

project / src / lib / metadata-lib.module.ts

import { NgModule } from '@angular/core';
import { MetadataLibComponent } from './metadata-lib.component';
import { MetadataBinidingDirective } from './metadata-biniding.directive';
import { ColumnBindingDirective } from './column-binding.directive';
import { CommonModule } from '@angular/common';
import { DropDownsModule } from '@progress/kendo-angular-dropdowns';
import { GridModule } from '@progress/kendo-angular-grid';
import { ButtonsModule } from '@progress/kendo-angular-buttons';
import { InputsModule } from '@progress/kendo-angular-inputs';
import { ColumnHeaderControlComponent } from './column-header-control/column-header-control.component';
import { ForeignkeyFieldControlComponent } from './foreignkey-field-control/foreignkey-field-control.component';
import { IconFieldControlComponent } from './icon-field-control/icon-field-control.component';
import { ImageFieldControlComponent } from './image-field-control/image-field-control.component';
import { CheckboxFilterControlComponent } from './checkbox-filter-control/checkbox-filter-control.component';
import { DropdownFilterControlComponent } from './dropdown-filter-control/dropdown-filter-control.component';
import { SwitchFilterControlComponent } from './switch-filter-control/switch-filter-control.component';

@NgModule({
  imports: [
    CommonModule,
    DropDownsModule,
    GridModule,
    ButtonsModule,
    InputsModule
  ],
  declarations: [
    MetadataLibComponent,
    MetadataBinidingDirective,
    ColumnBindingDirective,
    ColumnHeaderControlComponent,
    ForeignkeyFieldControlComponent,
    IconFieldControlComponent,
    ImageFieldControlComponent,
    CheckboxFilterControlComponent,
    DropdownFilterControlComponent,
    SwitchFilterControlComponent,
    ],
  exports: [
    MetadataLibComponent,
    MetadataBinidingDirective,
    ColumnBindingDirective,
    ColumnHeaderControlComponent,
    ForeignkeyFieldControlComponent,
    IconFieldControlComponent,
    ImageFieldControlComponent,
    CheckboxFilterControlComponent,
    DropdownFilterControlComponent,
    SwitchFilterControlComponent,
  ]
})
export class MetadataLibModule { }

我想知道我们是否可以在angular库指令中使用3rd party组件?

1 个答案:

答案 0 :(得分:0)

最后我想,是的,我们可以。 我花了永远的时间来正确配置它。

所以答案是肯定的。怎么?

library module.ts非常关键,您需要在import:[]中导入第三者模块,并且无论何时使用它,您都不应该相信Visual Studio代码建议路径。您需要手动设置它破坏了我的代码 这个

import { ColumnComponent } from '@progress/kendo-angular-grid';

原来是

import { ColumnComponent } from '@progress/kendo-angular-excel-export';

所以总结一下: 1,是的,我们可以使用3rd party,也可以在angular库中使用bindingDirectives AKA属性指令。我还不确定管道。我无法使用角度库中的管道解决问题。 2.在开发角度库时不要相信IDE的建议路径,