4

I have an app I am trying to upgrade to angular 5 and library-tize. I setup ng-packagr so the lib is built and bundled, this is no problem until I try to use one of the components with ngIf or ngFor which give me the error

HeaderComponent.html:13 ERROR Error: StaticInjectorError[ViewContainerRef]: 
  StaticInjectorError[ViewContainerRef]: 
    NullInjectorError: No provider for ViewContainerRef!
    at _NullInjector.get (core.js:993)

at runtime. I have looked around and tried some different things but nothing seems to fix it...

Here is relevant module code for the library.

@NgModule({
imports: [
    CommonModule,
    FormsModule,
    RouterModule,

    /* Bootstrap Imports */
    AccordionModule, 
    AlertModule, 
    ButtonsModule, 
    CarouselModule, 
    CollapseModule, 
    BsDropdownModule, 
    ModalModule, 
    PaginationModule, 
    ProgressbarModule, 
    RatingModule, 
    TabsModule, 
    TimepickerModule, 
    TooltipModule, 
    TypeaheadModule, 
    DatepickerModule,

    ServicesModule,
],
declarations: [
    LayoutComponent,
    HeaderComponent,
    FooterComponent,
    OffsidebarComponent,
    SidebarComponent,
    UserblockComponent,
    OverlayComponent,
    BusyIndicatorComponent
],
exports: [
    LayoutComponent
],
providers: [
    UserblockService
]
})
export class ComponentsModule {}

Here is the HTML where the error is reported.

          <a class="hidden-xs" trigger-resize="" (click)="toggleCollapsedSideabar()" *ngIf="!isCollapsedText()">
            <i class="material-icons">menu</i>
          </a>
comwizz2
  • 106
  • 1
  • 11
  • 1
    I've had this problem in the past and resolved it by: https://github.com/angular/angular-cli/issues/11454 Hope it helps! – Web Dev Nov 21 '18 at 03:22

5 Answers5

5

You should add BrowserModule to your imports array.

@NgModule({
imports: [
    BrowserModule, <----- add this
    CommonModule,
    FormsModule,
    RouterModule,

More info here.

Uğur Dinç
  • 2,415
  • 1
  • 18
  • 25
2

Removing the dependencies from library's package.json before adding the library to the main app was the fix for me.

Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
  • Can you describe this more precisely, what you did? You deleted everything from the library's package.json and then added the library to your main app's npm-package? – Steven X Aug 06 '19 at 15:25
  • Yeah, exactly what you said. Remove all the dependencies from your library's `package.json` file that you don't need in the consuming app. For example, I only have one and it's slider library. You don't need anything that the consuming app already has, i.e. the angular packages. – Kyle Conkright Aug 07 '19 at 17:31
0

Add the preserveSymlinks flag in the angular.json from your consuming App.

{"projects": {
    "MyApp": {
      "architect": {
        "build": {
          "options": {
            "preserveSymlinks": true, // <-----
[...]

And import the CommonModule in your Component Libary

Michael R.
  • 388
  • 3
  • 9
0

I've finally got my setup working by doing this:

  1. Having npm link setup properly ('npm link' in the library's dist folder and then 'npm link libname' in the main app's folder).
  2. Having preserveSymlinks set to true in the angular.json of the app that's consuming the library as @Michael R. mentioned

    
    
    

    {"projects": { "MyApp": { "architect": { "build": { "options": { "preserveSymlinks": true, // <----- [...]

3. Making sure i also had preserveSymlinks set to true in my library's tsconfig.lib.json



"angularCompilerOptions": {
                    "preserveSymlinks": true,, // <-----
        [...]

Recompiling the lib (with ng build) and recompiling the main app (on ng serve) had it working after this, also BrowserModule is supposed to be used in applicatins, use CommonModule inside libraries

Fred
  • 1
0

You can't use NgIf without importing CommonModule