"No provider for ViewContainerRef" 在使用 ng-packagr 组件时
"No provider for ViewContainerRef" while using ng-packagr component
我使用 angular cli ng generate library
创建了一个组件 库 ,
带有在模块内使用 *ngIf
的组件。
在我的主项目中成功构建和安装库后,
当我尝试使用该组件时出现 No provider 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 cli ng generate library
创建了一个组件 库 ,
带有在模块内使用 *ngIf
的组件。
在我的主项目中成功构建和安装库后,
当我尝试使用该组件时出现 No provider 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