作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 Angular cli nggeneratelibrary
创建了一个组件库,以及在模块内使用 *ngIf
的组件。
在我的主项目中成功构建并安装库后,当我尝试使用该组件时,出现 Noprovider for ViewContainerRef
错误。
版本:
@angular/cli 版本:“~7.0.6”,
@Angular/* 版本:“~7.0.0”
错误:
ERROR Error: StaticInjectorError(AppModule)[NgIf -> ViewContainerRef]: StaticInjectorError(Platform: core)[NgIf -> ViewContainerRef]: NullInjectorError: No provider for ViewContainerRef!
组件:
import {Component, OnInit, Input} from '@angular/core';
@Component({
selector: 'al-card',
templateUrl: './card.component.html',
styleUrls: ['./card.component.scss']
})
export class CardComponent implements OnInit {
@Input() header: string;
@Input() text: string;
constructor() { }
ngOnInit() {
}
}
模板:
<div *ngIf="header">
</div>
模块:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { CardComponent } from './card.component';
@NgModule({
declarations: [CardComponent],
imports: [
CommonModule
],
exports: [CardComponent],
})
export class CardModule { }
最佳答案
我已经发现问题了,通过将 "preserveSymlinks": true
添加到我的主项目的 angular.json 文件中解决了这个问题。
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"preserveSymlinks": true,
...
这是我找到解决方案的地方: https://github.com/angular/angular-cli/issues/10896
关于angular - 使用 ng-packagr 组件时为 "No provider for ViewContainerRef",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53833985/
我是一名优秀的程序员,十分优秀!