3

我使用 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 { }
4

1 回答 1

9

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

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

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

于 2018-12-18T13:59:38.657 回答