3
votes

I have created a components library using angular cli ng generate library, with a component that uses *ngIf inside a module.

After successfully build and install the library in my main project, when I try to use the component I am getting No provider for ViewContainerRef error.

Versions:

@angular/cli version: "~7.0.6",

@angular/* version : "~7.0.0"

Error:

ERROR Error: StaticInjectorError(AppModule)[NgIf -> ViewContainerRef]: StaticInjectorError(Platform: core)[NgIf -> ViewContainerRef]: NullInjectorError: No provider for ViewContainerRef!

component:

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() {
  }

}

template:

<div *ngIf="header">

</div>

module:

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
It comes from CommonModule, be sure to import it correctly.user4676340
thanks for the quick response, I am importing the CommonModule.itay oded
be sure that you're importing the CommonModule into the same Module that you declare the component.3xGuy
this is my module: @NgModule({ declarations: [CardComponent], imports: [ CommonModule ], exports: [CardComponent], }) export class CardModule { }itay oded
are you using ViewContainerRef in the Component, or trying to use it in a Service?3xGuy

1 Answers

9
votes

I have found out the problem, it solved by adding "preserveSymlinks": true to my main project's angular.json file.

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

this is where I have found the solution: https://github.com/angular/angular-cli/issues/10896