使用ng-packagr组件时“没有ViewContainerRef提供程序”

时间:2018-12-18 13:21:34

标签: angular ng-packagr

我已经使用角度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 { }

1 个答案:

答案 0 :(得分:2)

我发现了问题, 通过在我的主项目的angular.json文件中添加"preserveSymlinks": true来解决。

"architect": {
        "build": {
          "builder": "@angular-devkit/build-angular:browser",
          "options": {
            "preserveSymlinks": true,
             ...

这是我找到解决方案的地方: https://github.com/angular/angular-cli/issues/10896

相关问题