gpt4 book ai didi

angular - RouterModule.forRoot 调用了两次

转载 作者:太空狗 更新时间:2023-10-29 17:11:16 28 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现延迟加载,但似乎遇到了一个问题,我收到了错误消息:

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

所以我有我的主要 app-routing.module.tsapp-module.ts,如下所示:

应用模块.ts

// External Dependencies
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

// Internal Dependencies
import { MaterialModule } from '../app/configuration/material/material.module';
import { SharedModule } from '../app/application/shared/shared.module';
import { RoutingModule } from '../app/configuration/routing/routing.module';
import { SettingsModule } from '../app/application/settings/settings.module';

import { AppComponent } from './app.component';

import { SearchService } from './application/shared/services/search.service';

@NgModule({
declarations: [AppComponent],
imports: [
BrowserAnimationsModule,
BrowserModule,
SharedModule,
RoutingModule,
MaterialModule,
HttpClientModule,
FormsModule,
],
providers: [ ],
bootstrap: [AppComponent]
})

export class AppModule { }

应用程序路由.ts

// External Dependencies
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { HttpModule } from '@angular/http';

const appRoutes: Routes = [
{ path: '', redirectTo: 'overview', pathMatch: 'full' },
{ path: 'overview', loadChildren: '../../application/overview/overview.module#OverviewModule' },
{ path: 'search', loadChildren: '../../application/search/search.module#SearchModule' },
{ path: 'policy/:id', loadChildren: '../../application/policy/policy.module#PolicyModule' },
{ path: 'claim/:id', loadChildren: '../../application/claim/claim.module#ClaimModule' },
{ path: 'settings', loadChildren: '../../application/settings/settings.module#SettingsModule' }
];

@NgModule({
imports: [
CommonModule,
RouterModule.forRoot(appRoutes)
],
exports: [
RouterModule
],
declarations: []
})

export class RoutingModule { }

这工作正常并且应用程序正确加载。这里的问题是,在 SharedModule 中,它有一些组件可以使用 routerLink 将用户重定向到一个新页面。

共享模块.ts

// External Dependencies
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CalendarModule } from 'primeng/calendar';
import { AgmCoreModule } from '@agm/core';
import { FroalaEditorModule, FroalaViewModule } from 'angular-froala-wysiwyg';
import { PdfViewerModule } from 'ng2-pdf-viewer';

// Internal Dependencies
import { MaterialModule } from '../../configuration/material/material.module';
import { RoutingModule } from '../../configuration/routing/routing.module';

import { NavbarTopComponent } from '../../application/shared/components/navbar-top/navbar-top.component';
import { NavbarSideComponent } from './components/navbar-side/navbar-side.component';
import { TemplateCardWComponent } from './components/template-card-w/template-card-w.component';
import { FilterPipe } from './pipes/filter.pipe';
import { StandardTableComponent } from './components/standard-table/standard-table.component';
import { OrderPipe } from '../shared/pipes/order.pipe';
import { ActionComponent } from './components/action/action.component';
import { GoogleMapComponent } from './components/google-map/google-map.component';
import { HtmlEditorComponent } from './components/html-editor/html-editor.component';
import { PdfViewerComponent } from './components/pdf-viewer/pdf-viewer.component';
import { KeyBindingPipe } from './pipes/key-binding.pipe';
import { StandardEditTableComponent } from './components/standard-edit-table/standard-edit-table.component';

@NgModule({
imports: [
CommonModule,
MaterialModule,
RoutingModule,
FormsModule,
CalendarModule,
AgmCoreModule,
FroalaEditorModule,
FroalaViewModule,
PdfViewerModule
],
declarations: [
NavbarTopComponent,
NavbarSideComponent,
TemplateCardWComponent,
FilterPipe,
StandardTableComponent,
OrderPipe,
ActionComponent,
GoogleMapComponent,
HtmlEditorComponent,
PdfViewerComponent,
KeyBindingPipe,
StandardEditTableComponent
],
exports: [
]
})

export class SharedModule { }

如您所见,我必须导入 RouterModule。如果我删除 RouterModule,应用程序将加载但不会重定向。如果我保留 RouterModule,应用程序将导致问题顶部的错误。

谁能帮我解决这个问题。

最佳答案

我收到这个错误是因为我不小心将根 AppModule 导入了我的延迟加载模块。这导致根 AppRoutingModule 在加载惰性加载模块时再次被调用,因此 RouterModule.forRoot 被调用了两次。

如果您确定您没有调用 RouterModule.forRoot 两次,那么这可能是问题的原因。检查您是否没有在延迟加载的模块中导入任何直接调用 RouterModule.forRoot 的模块或导入调用 RouterModule.forRoot 的模块。

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

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