簡體   English   中英

Angular 5:組件上的“ NullInjectorError:Renderer2沒有提供程序”

[英]Angular 5: “NullInjectorError: No provider for Renderer2” on Component

我喜歡為Angular 5構建自己的UI庫。我已經將自己定位於Angular Material,並試圖構建一個簡單的按鈕組件。 在編譯時,它工作正常,但是當我在項目中使用庫時,出現運行時錯誤NullInjectorError: No provider for Renderer2

我不知道怎么了 我是否需要導入特殊模塊或其他內容?

在下面,您將找到我的組件類:

import { FocusMonitor } from "@angular/cdk/a11y";
import { Platform } from "@angular/cdk/platform";
import { ChangeDetectionStrategy, Component, Directive, ElementRef, HostListener, OnDestroy, Renderer2, ViewEncapsulation } from "@angular/core";

/**
 * Base class for further classes.
 */
export class MyButtonBase {
    constructor(public _renderer: Renderer2, public _elementRef: ElementRef) { }
}

@Component({
    moduleId: module.id,
    selector: "button[my-button], button[my-secondary-button]",
    exportAs: "myButton",
    host: {
        "[disabled]": "disabled || null",
    },
    templateUrl: "button.html",
    styleUrls: ["button.css"],
    inputs: ["disabled"],
    encapsulation: ViewEncapsulation.None,
    preserveWhitespaces: false,
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class MyButton extends MyButtonBase implements OnDestroy {

    constructor(renderer: Renderer2, elementRef: ElementRef, private _platform: Platform, private _focusMonitor: FocusMonitor) {
        super(renderer, elementRef);
        this._focusMonitor.monitor(this._elementRef.nativeElement, this._renderer, true);
    }

    public ngOnDestroy(): void {
        this._focusMonitor.stopMonitoring(this._elementRef.nativeElement);
    }

    public focus(): void {
        this.getHostElement().focus();
    }

    private getHostElement(): any {
        return this._elementRef.nativeElement;
    }
}

webpack似乎有問題。 從本地文件路徑安裝它時,會發生此錯誤。 當我發布它並通過npm安裝它時,它可以完美運行。

更新構造函數(renderer:Renderer2到構造函數(私有渲染器:Renderer2嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM