我有两个路由模块第一个应用程序路由模块,这是主要的一个,我试图懒惰加载食谱模块,总共我有6个模块,我会提供所有这些,我得到这个错误:错误:NG 04007:路由器提供了不止一次。如果在根注入器之外使用“forRoot”,则可能发生这种情况。当我尝试到达recipes路径时,即使我在recipes-routing-module中使用了RouterModule. forChild(),延迟加载的模块也应该使用RouterModule. forChild()。
应用模块:
import { ShoppingListModule } from './shopping-list.module';
import { RecipesModule } from './recipes.module';
import { AuthInterceptorService } from './services/auth-interceptor.service';
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { AuthComponent } from './auth/auth.component';
import { SharedModule } from './shared.module';
@NgModule({
declarations: [
AppComponent,
AuthComponent
],
imports: [
BrowserModule,
AppRoutingModule,
RecipesModule,
ShoppingListModule,
SharedModule,
],
providers: [{provide: HTTP_INTERCEPTORS, useClass: AuthInterceptorService, multi: true}],
bootstrap: [AppComponent]
})
export class AppModule { }
AppRoutingModule:
import { ErrorPageComponent } from './error-page/error-page.component';
import { ShoppingListComponent } from './shopping-list/shopping-list.component';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { AuthComponent } from './auth/auth.component';
const routes: Routes = [
{ path: 'shopping-list', component: ShoppingListComponent },
{ path: 'not-found', component: ErrorPageComponent, data: { message: 'Page Not Found' } },
{ path: 'auth', component: AuthComponent },
{ path: 'recipes', loadChildren: () => import('./recipes.module').then(m => m.RecipesModule) }
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule],
})
export class AppRoutingModule {}
RecipesRoutingModule:
import { RouterModule, Routes } from '@angular/router';
import { NgModule } from '@angular/core';
import { CreateRecipeComponent } from './recipes/create-recipe/create-recipe.component';
import { RecipesDetailComponent } from './recipes/recipes-detail/recipes-detail.component';
import { RecipesComponent } from './recipes/recipes.component';
import { CanActivateGuardService } from './services/guards/can-activate-guard.service';
import { CanDeactivateGuardService } from './services/guards/can-deactivate-guard.service';
import { RecipesResolverService } from './services/guards/recipes-resolver.service';
const routes: Routes = [
{
path: '',
component: RecipesComponent,
canActivate: [CanActivateGuardService],
children: [
{
path: 'create-recipe',
component: CreateRecipeComponent,
canDeactivate: [CanDeactivateGuardService],
},
{
path: ':id',
component: RecipesDetailComponent,
resolve: [RecipesResolverService],
},
{
path: ':id/edit',
component: CreateRecipeComponent,
canDeactivate: [CanDeactivateGuardService],
resolve: [RecipesResolverService],
},
{
path: '',
redirectTo: 'recipes-list',
pathMatch: 'full',
},
],
},
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class RecipesRoutingModule {}
共享模块:
import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { DropdownDirective } from './directives/dropdown.directive';
import { ErrorPageComponent } from './error-page/error-page.component';
import { HeaderComponent } from './header/header.component';
import { NotFoundComponent } from './not-found/not-found.component';
import { PlaceholderDirective } from './directives/placeholder.directive';
import { ErrorModalComponent } from './reuseable/components/error-modal/error-modal.component';
import { LoadingSpinnerComponent } from './reuseable/components/spinner/loading-spinner/loading-spinner.component';
import { AppRoutingModule } from './app-routing.module';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
@NgModule({
declarations: [
HeaderComponent,
DropdownDirective,
NotFoundComponent,
ErrorPageComponent,
LoadingSpinnerComponent,
ErrorModalComponent,
PlaceholderDirective
],
imports: [
CommonModule,
AppRoutingModule,
ReactiveFormsModule,
FormsModule,
HttpClientModule,
],
exports: [
HeaderComponent,
DropdownDirective,
NotFoundComponent,
ErrorPageComponent,
LoadingSpinnerComponent,
ErrorModalComponent,
PlaceholderDirective,
CommonModule,
ReactiveFormsModule,
FormsModule
]
})
export class SharedModule { }
RecipesModule:
import { RecipesRoutingModule } from './recipes-routing.module';
import { ReactiveFormsModule } from '@angular/forms';
import { NgModule } from '@angular/core';
import { CreateRecipeComponent } from './recipes/create-recipe/create-recipe.component';
import { RecipesDetailComponent } from './recipes/recipes-detail/recipes-detail.component';
import { RecipeItemComponent } from './recipes/recipes-list/recipe-item/recipe-item.component';
import { RecipesListComponent } from './recipes/recipes-list/recipes-list.component';
import { RecipesComponent } from './recipes/recipes.component';
import { SharedModule } from './shared.module';
@NgModule({
declarations: [
RecipesComponent,
RecipesListComponent,
RecipesDetailComponent,
RecipeItemComponent,
CreateRecipeComponent
],
imports: [
RecipesRoutingModule,
SharedModule
],
// exports: [
// RecipesComponent,
// RecipesListComponent, //Because now we load these components internaly via routing(in the recipeRoutingModule) we dont need to export the components
// RecipesDetailComponent,
// RecipeItemComponent,
// CreateRecipeComponent
// ]
})
export class RecipesModule { }
ShoppingListModule:
import { NgModule } from '@angular/core';
import { ShoppingListComponent } from './shopping-list/shopping-list.component';
import { ShoppingListEditComponent } from './shopping-list/shopping-list-edit/shopping-list-edit.component';
import { SharedModule } from './shared.module';
@NgModule({
declarations: [ShoppingListComponent, ShoppingListEditComponent],
imports: [SharedModule],
exports: [ShoppingListComponent, ShoppingListEditComponent]
})
export class ShoppingListModule {}
1条答案
按热度按时间monwx1rj1#
正如错误所述,您导入了两次
RouterModule.forRoot()
。您可以将其导入
AppRoutingModule
。然后,在AppModule
和SharedModule
中导入AppRoutingModule
。在AppModule中,您实际上导入了
SharedModule
(它导入了自身AppRoutingModule
)和AppRoutingModule
。您应该只导入一次路由。您应该从
SharedModule
中删除AppRoutingModule
。