1


I want to create some route with lazy load. But I can't get rid of error

Cannot find module app/search/search.module

Folder "search" is in the "app" folder, the path is correct. It works without webpack and this trouble starts after webpack configuring.
It works without lazy load.
Where is the problem?

app.module.ts

import { NgModule }      from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent }  from './app.component';
import { RouterModule, Routes } from '@angular/router';

const appRoutes: Routes = [
  { path: 'search', loadChildren: 'app/search/search.module#SearchModule' },
  { path: '**', redirectTo: '/search' }
];

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(appRoutes)
  ],
  declarations: [ AppComponent ],
  bootstrap:    [ AppComponent ]
})
export class AppModule { }

search.module.ts

import { NgModule }      from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { SearchComponent } from "./search.component";
import { LocationsComponent } from "./locations/locations.component";
import { CommonModule } from "@angular/common";
import { GoogleMapsService } from "../shared/services/google-maps.service";

const routes: Routes = [
  { path: '', component: SearchComponent },
  { path: '**', redirectTo: '' }
];

@NgModule({
  imports: [
    CommonModule,
    RouterModule.forChild(routes)
  ],
  exports: [
    RouterModule
  ],
  declarations: [ SearchComponent, LocationsComponent ],
  providers: [ GoogleMapsService ]
})
export class SearchModule { }

2 Answers 2

1

Try this

const routes: Routes = [
        {path: '', component: SearchComponent, children: [
            {path: '**', redirectTo: ''}
        ]}
    ]
Sign up to request clarification or add additional context in comments.

Comments

0

I think you should include your search.module.ts into the app.module.ts and put SearchModule in declarations.

Or try adding ./ to this { path: 'search', loadChildren: 'app/search/search.module#SearchModule' },. In order to have as a result this:

{ path: 'search', loadChildren: './app/search/search.module#SearchModule' },

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.