我确实有问题。我正在 Angular 库中构建一个组件:
<button
class="btn btn-white btn-home"
[class]="getClassBySeverity()"
[disabled]="disabled"
[hidden]="!renderer"
[ngClass]="{ uppercase: uppercase }"
[type]="type"
[style.color]="labelColor"
[style.backgroundColor]="backgroundColor"
>
<span [hidden]="!hasIcon()">
<span
[ngClass]="{ 'btn-icon-left': icon }"
[hidden]="!isLeftPosition()"
[class]="icon"
></span
>{{ label
}}<span
[hidden]="isLeftPosition()"
[ngClass]="{ 'btn-icon-right': icon }"
[class]="icon"
></span>
</span>
<span [hidden]="hasIcon()">{{ label }}</span>
</button>
但是当我在我的应用程序中嵌入这个组件时,我得到了:
错误 NullInjectorError: StaticInjectorError(AppModule)[NgClassImpl -> ElementRef]: StaticInjectorError(Platform: core)[NgClassImpl -> ElementRef]: NullInjectorError: 没有提供 ElementRef!
发生这种情况是因为ngClass。
如果我在应用程序中构建我的组件,它工作正常。
我在 github 的一个问题中看到我需要将preserveSymlinks放在我的tsconfig.lib.json中:
"angularCompilerOptions": {
"annotateForClosureCompiler": true,
"skipTemplateCodegen": true,
"strictMetadataEmit": true,
"fullTemplateTypeCheck": true,
"strictInjectionParameters": true,
"enableResourceInlining": true,
"preserveSymlinks": true
}
但无论如何它都不起作用。
任何人都知道在 Angular 中构建库的正确方法吗?
谢谢!