【问题标题】:"No provider for ViewContainerRef" while using ng-packagr component使用 ng-packagr 组件时“没有 ViewContainerRef 的提供者”
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

  • Angluar 8 不存在,怎么会这样?
  • @itay oded 它不适用于 ng serve,有什么想法吗?我认为这是 --prod 个人资料
  • 使用 Angular 8 和 ng serve 为我工作。
  • 这解决了我在尝试将 bti 用于 angular 时的问题!谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-06
  • 2021-12-15
  • 2017-08-13
  • 1970-01-01
相关资源
最近更新 更多