Skip to content
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
This repository was archived by the owner on Apr 8, 2020. It is now read-only.

Angular-CLI-Based-Template: Lazy Loading doesnt work with SSR #1413

Closed
@naveedahmed1

Description

@naveedahmed1

@SteveSandersonMS with new Angular CLI based template the Lazy Loading doesn't work when we enable SSR. The server log shows below error:

Microsoft.AspNetCore.NodeServices:Error: ERROR { Error: Uncaught (in promise): TypeError: Cannot read property './counter/counter.module#CounterModule' of undefined
TypeError: Cannot read property './counter/counter.module#CounterModule' of undefined
    at ModuleMapNgFactoryLoader.load (D:\MyProject\ClientApp\node_modules\@nguniversal\module-map-ngfactory-loader\src\module-map-ngfactory-loader.js:18:34)

To reproduce this error

Convert the Counter section to Lazy Module

import { CommonModule } from '@angular/common';
import { RouterModule } from '@angular/router';

import { NgModule } from '@angular/core';

import { CounterComponent } from './counter.component';

@NgModule({
    imports: [
        CommonModule,
        
        RouterModule.forChild([
          { path: '', component: CounterComponent }
        ])
    ],
    declarations: [
      CounterComponent
    ]
})
export class CounterModule { }

Update AppModule, remove Counter Component from Declarations array and update route for counter url.

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { RouterModule } from '@angular/router';

import { AppComponent } from './app.component';
import { NavMenuComponent } from './nav-menu/nav-menu.component';
import { HomeComponent } from './home/home.component';
import { FetchDataComponent } from './fetch-data/fetch-data.component';

@NgModule({
  declarations: [
    AppComponent,
    NavMenuComponent,
    HomeComponent,
    FetchDataComponent
  ],
  imports: [
    BrowserModule.withServerTransition({ appId: 'ng-cli-universal' }),
    HttpClientModule,
    FormsModule,
    RouterModule.forRoot([
      { path: '', component: HomeComponent, pathMatch: 'full' },
      { path: 'counter', loadChildren: './counter/counter.module#CounterModule' },
      { path: 'fetch-data', component: FetchDataComponent },
    ])
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

I think the template is missing ts-loader

Ref: https://github.com/angular/angular-cli/wiki/stories-universal-rendering#install-dependencies

And webpack.server.config.js

https://github.com/angular/angular-cli/wiki/stories-universal-rendering#webpackserverconfigjs-root-project-level

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions