gpt4 book ai didi

javascript - Angular 延迟加载模块错误 - 'RouterModule.forRoot() called twice'

转载 作者:行者123 更新时间:2023-12-01 01:56:12 25 4
gpt4 key购买 nike

我正在我的 Angular 6 应用程序中实现延迟加载的功能模块,并已成功配置一个“发票”功能,但在为延迟加载的“费用”和“联系人”实现路由时遇到问题功能模块,其设置方式与第一个模块相同。

每个模块都已导入到 AppModule 中,并且它们还使用 SharedModule,我已将其导入到 AppModule 和每个功能模块中。

使用“费用”或“联系人”模块路由到任何页面时,我在控制台中收到以下错误:

ERROR Error: Uncaught (in promise): Error: RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead. Error: RouterModule.forRoot() called twice. Lazy loaded modules should use RouterModule.forChild() instead. at provideForRootGuard (vendor.js:106249)

正在使用 .forChild(routes) 作为功能模块,但我能想到的唯一可能导致此问题的是过程中某个地方的困惑导入。根据之前关于其他人遇到此问题的问题,我检查了 AppModule 是否已导入到任何其他模块中,从而导致 forRoot() 被调用两次,但事实并非如此。

由于错误表明与provideForRootGuard有关,我认为这可能与将CanActivateRootGuard导入到每个模块有关,但删除它也没有解决问题。

应用程序路由模块:

import { NgModule } from '@angular/core';
import { RouterModule, Routes, RouterLinkActive } from '@angular/router';
import { CanActivateRouteGuard } from './can-activate-route.guard';

// COMPONENTS
// Dashboard
import { DashboardComponent } from './dashboard/dashboard.component';
// Login
import { LoginComponent } from './login/login.component';
// Register
import { RegisterComponent } from './register/register.component';
// Notifications
import { NotificationsComponent } from './notifications/notifications.component';
// Bank
import { BankComponent } from './bank/bank.component';
// Documents
import { DocumentsComponent } from './documents/documents.component';

const routes: Routes = [
{
path: '',
redirectTo: 'login',
pathMatch: 'full'
},
{
path: 'dashboard',
component: DashboardComponent,
canActivate: [CanActivateRouteGuard]
},

// Login/Register
{
path: 'login',
component: LoginComponent
},
{
path: 'register',
component: RegisterComponent
},

// Notifications
{
path: 'notifications',
component: NotificationsComponent,
canActivate: [CanActivateRouteGuard]
},
{
path: 'notifications/:id',
component: NotificationsComponent,
canActivate: [CanActivateRouteGuard]
},

// Bank
{
path: 'bank',
component: BankComponent,
canActivate: [CanActivateRouteGuard]
},

// Contacts
{
path: 'contacts',
loadChildren: './contacts/contacts.module#ContactsModule'
},

// Expenses
{
path: 'expenses',
loadChildren: './expenses/expenses.module#ExpensesModule'
},

// Invoices
{
path: 'invoices',
loadChildren: './invoices/invoices.module#InvoicesModule'
},

// Documents
{
path: 'documents',
component: DocumentsComponent,
canActivate: [CanActivateRouteGuard]
}
]

@NgModule ({

imports: [
RouterModule.forRoot(routes)
],

exports: [
RouterModule
]

})

应用程序模块

// ANGULAR CORE
import { BrowserModule } from '@angular/platform-browser';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NgModule } from '@angular/core';

// FEATURE MODULES
import { ContactsModule } from '@app/contacts/contacts.module';
import { ExpensesModule } from '@app/expenses/expenses.module';
import { InvoicesModule } from '@app/invoices/invoices.module';
import { BankModule } from '@app/bank/bank.module';
import { DocumentsModule } from '@app/documents/documents.module';

// MATERIAL MODULE
import { MaterialModule } from '@app/material.module';

// SHARED MODULE
import { SharedModule } from '@app/shared.module';

// COMPONENTS
import { AppComponent } from './app.component';
import { DashboardComponent } from './dashboard/dashboard.component'

// Account
import { LoginComponent } from './login/login.component'
import { RegisterComponent } from './register/register.component'
import { VerifyEmailDialogComponent } from './register/dialogs/verify-email-dialog/verify-email-dialog.component';

// Notifications
import { NotificationsComponent } from './notifications/notifications.component';

@NgModule({

declarations: [
AppComponent,

// COMPONENTS
// Dashboard
DashboardComponent,
// Login
LoginComponent,
// Register
RegisterComponent,
// Dialogs
VerifyEmailDialogComponent,
// Notifications
NotificationsComponent
],

imports: [
// ANGULAR CORE
BrowserModule,
BrowserAnimationsModule,

// FEATURE MODULES
InvoicesModule,
ContactsModule,
ExpensesModule,
BankModule,
DocumentsModule,

// MATERIAL MODULE
MaterialModule,

// SHARED MODULE
SharedModule
],

entryComponents: [
// DIALOGS
// Register
VerifyEmailDialogComponent
],

providers: [

],

bootstrap: [AppComponent]
})

export class AppModule { }

费用路由模块

import { NgModule } from '@angular/core';
import { RouterModule, Routes, RouterLinkActive } from '@angular/router';
// import { CanActivateRouteGuard } from '@app/can-activate-route.guard';

// COMPONENTS
import { NewExpenseComponent } from './new-expense/new-expense.component';
import { ExpenseListComponent } from './expense-list/expense-list.component';
import { ViewExpenseComponent } from './view-expense/view-expense.component';

const routes: Routes = [
{
path: 'expenses/new',
component: NewExpenseComponent,
// canActivate: [CanActivateRouteGuard]
},
{
path: 'expenses/all',
component: ExpenseListComponent,
// canActivate: [CanActivateRouteGuard]
},
{
path: 'expenses/:id',
component: ViewExpenseComponent,
// canActivate: [CanActivateRouteGuard]
},
]

@NgModule({

imports: [
RouterModule.forChild(routes)
],

exports: [
RouterModule
]

})

export class ExpensesRoutingModule {

}

费用模块

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import { ExpensesRoutingModule } from './expenses-routing.module';

// SHARED/MATERIAL MODULES
import { SharedModule } from '@app/shared.module';
import { MaterialModule } from '@app/material.module';

// COMPONENTS
// New Expense
import { NewExpenseComponent } from './new-expense/new-expense.component';
// Expense List
import { ExpenseListComponent } from './expense-list/expense-list.component';
// View Expense
import { ViewExpenseComponent } from './view-expense/view-expense.component';
// Dialogs
import { DeleteExpenseDialogComponent } from './view-expense/dialogs/delete-expense-dialog/delete-expense-dialog.component';

@NgModule({

imports: [
CommonModule,
ExpensesRoutingModule,
SharedModule,
MaterialModule
],

declarations: [
// COMPONENTS
// New Expense
NewExpenseComponent,
// Expense List
ExpenseListComponent,
// View Expense
ViewExpenseComponent,
// Dialogs
DeleteExpenseDialogComponent
],

entryComponents: [
// DIALOGS
// View Expense
DeleteExpenseDialogComponent
]

})

export class ExpensesModule {

}

SharedModule 路由导入

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

// ROUTING
import { AppRoutingModule } from './app-routing.module';
import { RouterLinkActive, CanActivate } from '@angular/router';
import { CanActivateRouteGuard } from './can-activate-route.guard';

...

最佳答案

修复

设法通过从 SharedModule 中删除 AppRoutingModule 并将其移至 AppModule 来解决此问题。所有路线现在工作正常。

关于javascript - Angular 延迟加载模块错误 - 'RouterModule.forRoot() called twice',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51020017/

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