【发布时间】:2019-05-18 22:15:31
【问题描述】:
我使用 Angular cli ng generate library 创建了一个组件库,
使用在模块内使用 *ngIf 的组件。
在我的主项目中成功构建并安装库后,
当我尝试使用该组件时,我收到No provider for ViewContainerRef 错误。
版本:
@angular/cli 版本:“~7.0.6”,
@angular/* 版本:“~7.0.0”
错误:
错误错误:StaticInjectorError(AppModule)[NgIf -> ViewContainerRef]: StaticInjectorError(平台:核心)[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 { }
【问题讨论】:
-
来自
CommonModule,一定要正确导入。 -
感谢您的快速响应,我正在导入
CommonModule。 -
确保将
CommonModule导入到声明组件的同一模块中。 -
这是我的模块:@NgModule({ declarations: [CardComponent], imports: [CommonModule], exports: [CardComponent], }) export class CardModule { }
-
您是在组件中使用
ViewContainerRef,还是尝试在服务中使用它?
标签: angular ng-packagr