gpt4 book ai didi

javascript - RouterModule.forRoot() 调用了两次

转载 作者:搜寻专家 更新时间:2023-10-30 21:58:25 25 4
gpt4 key购买 nike

我正在尝试在我的 Angular 5 应用程序中实现延迟加载,但出现错误:

未捕获( 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 {}

关于javascript - RouterModule.forRoot() 调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50450000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com