2
votes

I'm trying to implement lazyloading into my angular 5 app, but i get error:

Uncaught (in promise): Error: RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead.

There my routing modules: app-routing.module.ts

import { NgModule } from "@angular/core";
import { RouterModule, Routes } from "@angular/router";

// import Commponents for router

import { MainPageComponent } from "../pages/main/main-page.component";
import { AboutCompanyPageComponent } from "../pages/about-company/about-company-page.component";
import { MediaPageComponent } from "../pages/media/media-page.component";
import { ChangePasswordComponent } from "../components/change-password/change-password.component";
import { ConfirmOrderComponent } from "../pages/confirm-order/confirm-order.component";
import { NoConnectionComponent } from "../pages/no-connection/no-connection.component";
import { NotFoundComponent } from "../pages/not-found/not-found.component";
import { SearchComponent } from "../pages/search/search.component";
import { PaymentComponent } from "../pages/payment/payment.component";
import { WorkersGeoLocationComponent } from "../pages/workers-geo-location/workers-geo-location.component";
import { AdminProfileModule } from "../modules/admin-profile/admin-profile.module";

const routes: Routes = [
    {
        path: "",
        component: MainPageComponent
    },
    {
        path: "about",
        component: AboutCompanyPageComponent
    },
    {
        path: "payment",
        component: PaymentComponent
    },
    {
        path: "media",
        component: MediaPageComponent
    }, {
        path: "profile/workersGeoLocation",
        component: WorkersGeoLocationComponent
    },
    {
        path: "admin",
        loadChildren: '../modules/admin-profile/admin-profile.module#AdminProfileModule'
        // loadChildren: () => AdminProfileModule
    },
    {
        path: "password/recovery",
        component: ChangePasswordComponent
    },
    {
        path: "search",
        component: SearchComponent
    },
    {
        path: "confirm",
        component: ConfirmOrderComponent
    },
    {
        path: "no-connection",
        component: NoConnectionComponent
    },
    {
        path: "**",
        component: NotFoundComponent
    }
];

@NgModule({
    imports: [
        RouterModule.forRoot(routes)
    ],
    exports: [
        RouterModule
    ]
})
export class AppRoutingModule {
}

There I use forRoot() method for RouterModule.

In my second module, which should be lazy loaded I use forChild() method,

There some code:

import { NgModule } from '@angular/core';
import { RouterModule, Routes } from "@angular/router";
import { OrdersTabComponent } from "../../components/tabs/orders-tab/orders-tab.component";

const routes: Routes = [
    {
        path: '',
        component: OrdersTabComponent
    }
];

@NgModule({
    imports: [
        RouterModule.forChild(routes)
    ],
    exports: [
        RouterModule
    ]
})

export class AdminRoutingModule { }

And as u can see there are only two modules with separate methods for RouterModule and I can't understand where is mistake.

Can anybody help me with it?

1
Did you ever find a solution to this?inorganik
@inorganik nope. just refactored my codeEgor Zvuzdetskyi
Check that you're not importing the AppModule somewhere (you should never export or import the AppModule)inorganik
@inorganik thanks for your answer! But currently I don't have this problem anymore, I would be care full of this next timeEgor Zvuzdetskyi

1 Answers

0
votes

//you should be use like below using proper child module path

          import { NgModule }             from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { HashLocationStrategy ,LocationStrategy } from '@angular/common';

import { SelectivePreloadingStrategy } from './selective-preloading-strategy';
import { NotFoundComponent } from './not-found/not-found.component';
import { IndexComponent } from './index/index.component';

const routes: Routes = [
{ path: "",    component: IndexComponent},
{ path: "admin", loadChildren: "app/admin/admin-layout/admin-layout.module#AdminLayoutModule" },
{ path: "**",    component: NotFoundComponent }
];
@NgModule({
  imports: [ RouterModule.forRoot(
      routes,
      { preloadingStrategy: SelectivePreloadingStrategy }
  )],
  exports: [ RouterModule ],
  providers: [
      { provide: LocationStrategy, useClass: HashLocationStrategy },
  ]
})
export class AppRoutingModule {}