我使用 angular cli创建了一个组件库ng generate library
,其中一个组件*ngIf
在模块内部使用。
在我的主项目中成功构建并安装库后,当我尝试使用该组件时No provider for ViewContainerRef
出现错误。
版本:
@angular/cli 版本:“~7.0.6”,
@angular/* 版本:“~7.0.0”
错误:
ERROR 错误:StaticInjectorError(AppModule)[NgIf -> ViewContainerRef]: StaticInjectorError(Platform: core)[NgIf -> ViewContainerRef]: NullInjectorError: 没有 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 { }