使用构造函数中的ElementRef从angular app访问angular库-angular组件中的StaticInjectorError
我通过以下方式创建了angular库(名为ng库)使用构造函数中的ElementRef从angular app访问angular库-angular组件中的StaticInjectorError,angular,angular-library,ng-modules,Angular,Angular Library,Ng Modules,我通过以下方式创建了angular库(名为ng库) 使用ng build ng库构建 使用纱线链接 创建一个角度应用程序,并使用纱线链接ng库链接库 我可以使用模块内的组件和服务。一切都好 现在我在ng库中添加了一个新的角度组件,它使用ElementRef import { Component, NgZone, Input, Output, forwardRef } from '@angular/core'; import { ChangeDetectionStrategy, Element
- 使用
ng build ng库构建
- 使用
纱线链接
- 创建一个角度应用程序,并使用
纱线链接ng库链接库
import { Component, NgZone, Input, Output, forwardRef } from '@angular/core';
import { ChangeDetectionStrategy, ElementRef, EventEmitter } from '@angular/core';
@Component({
selector: 'new-component',
template: '<ng-content></ng-content>'
})
export class NewComponent {
protected el: HTMLElement;
constructor(r: ElementRef, protected z: NgZone) {
this.el = r.nativeElement;
}
}
这就是我在Angular应用程序中添加模块的方式
import { NgLibraryModule } from "ng-library";
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, NgLibraryModule.forRoot()],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}
错误表示ElementRef的提供程序不可用。如何使ElementRef在角度库中的NgModule中可用
谢谢
Basanth您需要将
ElementRef
与@ViewChild
装饰器一起使用:
例子:
@ViewChild('idOfTheDiv/ButtonAsInYourHTML',{static:true})私有idOfTheDiv/Button:ElementRef代码>
你可以在这里读更多
import { NgLibraryModule } from "ng-library";
@NgModule({
declarations: [AppComponent],
imports: [BrowserModule, AppRoutingModule, NgLibraryModule.forRoot()],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule {}