0

I am using asp-net core and angular 2 a web project. I want to implement module lazy loading concept in my current application but its not working for me tried almost every solution available.

ClientApp\app\components\home\ home.routing.module.ts

const routes: Routes = [
    {
        path: 'home', component: HomeComponent,
        children: [
            { path: '', redirectTo: 'dashboard', pathMatch: 'full' },
            { path: 'dashboard', component: DashboardComponent }
        ]
    }
]

export const routableComponents = [DashboardComponent]
@NgModule({
    imports: [CommonModule, RouterModule.forChild(routes)],
    exports: [RouterModule],
    declarations: [routableComponents]
})
export class HomeRoutingModule {
}

app-routing-module.ts

const routes: Routes = [
    { path: '', redirectTo: 'login', pathMatch: 'full' },
    { path: 'login', component: LoginComponent },
    { path: 'register-user', component: RegisterUserComponent },
    { path: 'home', loadChildren: "./components/home/home.routing.module#HomeRoutingModule" },
    { path: '**', redirectTo: 'login' }

i am getting the this error with above mentioned code

EXCEPTION: Uncaught (in promise): TypeError: webpack_require.i(...) is not a function TypeError: webpack_require.i(...) is not a function

also using this package as per suggested here

"angular2-router-loader": "0.3.4"

webpack.config.js

const sharedConfig = {
        stats: { modules: false },
        context: __dirname,
        resolve: { extensions: ['.js', '.ts'] },
        output: {
            filename: '[name].js',
            publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
        },
        module: {
            rules: [
                {
                    test: /\.ts$/, include: /ClientApp/, use: ['awesome-typescript-loader?silent=true', 'angular2-template-loader',
                    'angular2-router-loader']
                },
                { test: /\.html$/, use: 'html-loader?minimize=false' },
                { test: /\.css$/, use: ['to-string-loader', 'css-loader'] },
                { test: /\.(png|jpg|jpeg|gif|svg)$/, use: 'url-loader?limit=25000' }
            ]
        },
        plugins: [new CheckerPlugin()]
    }

Please suggest what i am doing wrong.

3
  • The path to your home routing may need some assistance. This could be a lack of understanding on my part, but in a project I have lazy loading in I have the following... { path: 'recipes', loadChildren: 'app/recipes/recipes.module#RecipesModule' }, notice it gives the path starting with app and doesn't have the ./ in the front. Try that? Commented Feb 10, 2017 at 16:16
  • Also one thing you have failed to mention is what process you're running that gets you that error. Are you attempting to build / compile? Are you doing AoT? Are you just trying to get a local server running through npm run <command> or what? Commented Feb 10, 2017 at 16:19
  • after user login to app i am trying to redirect to home page which i am loading through lazy load that's where its not working Commented Feb 10, 2017 at 16:21

2 Answers 2

1

Check you webpack configuration should be like that , probably you missed the loaders for the webpack

    // Configuration in common to both client-side and server-side bundles
var sharedConfig = {
    context: __dirname,
    resolve: { extensions: [ '', '.js', '.ts' ] },
    output: {
        filename: '[name].js',
        publicPath: '/dist/' // Webpack dev middleware, if enabled, handles requests for this URL prefix
    },
    module: {
        loaders: [
            { test: /\.ts$/, include: /ClientApp/, loaders: ['ts-loader?silent=true', 'angular2-template-loader', 'angular2-router-loader'] },
            { test: /\.html$/, loader: 'html-loader?minimize=false' },
            { test: /\.css$/, loader: 'to-string-loader!css-loader' },
            { test: /\.(png|jpg|jpeg|gif|svg)$/, loader: 'url-loader', query: { limit: 25000 } },
            { test: /\.json$/, loader: 'json-loader' }
        ]
    }
Sign up to request clarification or add additional context in comments.

3 Comments

i have it like : use: ['awesome-typescript-loader?silent=true', 'angular2-template-loader', 'angular2-router-loader'] instead of loaders
updated my question, you can check the complete configuration
i am using "webpack": "^2.2.0", as per new webpack module.loaders is now module.rules, webpack.js.org/guides/migrating
0

It works for me.

Restart your server: ng serve --aot

cli needs to be reloaded when a lazy loaded module is added in routes.

https://github.com/angular/angular-cli/issues/9825

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.