我试图在我的Angular5应用程序中实现懒散加载,但我得到了错误:
uncaught(在promise中):错误:routermodule.forroot()调用了两次。延迟加载的模块应改用routermodule.forchild()。
这里是我的路由模块:
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 {
}
在这里,我对routermodule使用forroot()方法。
在我的第二个模块中,我使用forchild()方法延迟加载,
这里有一些代码:
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 { }
正如你所看到的,只有两个模块有单独的routermodule方法,我不知道哪里是错误的。
有人能帮我吗?
最佳答案
//应该像下面这样使用正确的子模块路径
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 {}