简体   繁体   English

Angular中不同模块的正确布线方式是什么?

[英]What is the correct way of routing different modules in Angular?

I have a project with three modules: auth, libros and prestamo, every module has different components and his respective routing and I have a general routing module for al the project, the code in my "auth-routing-module" is the next:我有一个包含三个模块的项目:auth、libros 和 prestamo,每个模块都有不同的组件和他各自的路由,我有一个用于项目的通用路由模块,我的“auth-routing-module”中的代码是下一个:

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';
import { RouterModule, Routes } from '@angular/router';
import { AuthComponent } from '../auth/auth.component';
import { LoginComponent } from '../login/login.component';
import { ForgotPasswordComponent } from '../forgot-password/forgot-password.component';
import { SignUpComponent } from '../sign-up/sign-up.component';

const authRoutes: Routes = [
  { path: 'auth', component: AuthComponent},
  { path: 'login', component: LoginComponent},
  { path: 'forgotPassword', component: ForgotPasswordComponent},
  { path: 'signUp', component: SignUpComponent}
];

@NgModule({
  declarations: [],
  imports: [
    CommonModule, RouterModule.forRoot(authRoutes)
  ], 
  exports: [RouterModule]
})
export class AuthRoutingModule { }

I have a similar code in the other modules, and the code in my "routing-module" is the next:我在其他模块中有类似的代码,我的“路由模块”中的代码是下一个:

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

const appRoutes: Routes = [
  {path: 'auth', loadChildren:() => import('../auth/auth.module').then(mod => mod.AuthModule)},
  {path: 'libros', loadChildren:() => import('../libros/libros.module').then(mod => mod.LibrosModule)},
  {path: 'prestamos', loadChildren:() => import('../prestamos/prestamos.module').then(mod => mod.PrestamosModule)}
];


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

Finally this is the code of my "app-module":最后这是我的“应用程序模块”的代码:

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppRoutingModule } from './app-routing/app-routing.module';

import { AppComponent } from './app.component';
import { AuthModule } from './auth/auth.module';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule, AuthModule, AppRoutingModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Now, when I check the navigation with the auth components the navigation works, but when I try to use a component of other module I get the next error:现在,当我使用 auth 组件检查导航时,导航可以正常工作,但是当我尝试使用其他模块的组件时,出现下一个错误:

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 Object.provideForRootGuard [as useFactory] (router.js:5749)
    at Object.factory (core.js:11468)
    at R3Injector.hydrate (core.js:11385)
    at R3Injector.get (core.js:11205)
    at injectInjectorOnly (core.js:4728)
    at Module.ɵɵinject (core.js:4732)
    at Object.RouterModule_Factory [as factory] (router.js:5714)
    at R3Injector.hydrate (core.js:11385)
    at R3Injector.get (core.js:11205)
    at core.js:11242
    at resolvePromise (zone-evergreen.js:1213)
    at resolvePromise (zone-evergreen.js:1167)
    at zone-evergreen.js:1279
    at ZoneDelegate.invokeTask (zone-evergreen.js:406)
    at Object.onInvokeTask (core.js:28497)
    at ZoneDelegate.invokeTask (zone-evergreen.js:405)
    at Zone.runTask (zone-evergreen.js:178)
    at drainMicroTaskQueue (zone-evergreen.js:582)

These are my first practices with Angular and I have no idea what are the "Lazy loaded modules" can anyone help me?这些是我对 Angular 的第一次实践,我不知道什么是“延迟加载模块”,有人可以帮我吗?

You can actually make it as lazy loaded module by doing child routes in parent route module.您实际上可以通过在父路由模块中执行子路由将其作为延迟加载模块。 Refer this: https://angular.io/guide/lazy-loading-ngmodules this might help you请参考: https://angular.io/guide/lazy-loading-ngmodules这可能会对您有所帮助

Use RouterModule.forChild(authRoutes) in your AuthRoutingModule and other two Feature modules.AuthRoutingModule和其他两个功能模块中使用RouterModule.forChild(authRoutes)

forRoot() should only be used with your root module forRoot()只能与您的根模块一起使用

You are not supposed to call RouterModule.forRoot() multiple times in an application.您不应该在应用程序中多次调用RouterModule.forRoot()

.forRoot() should be called only for the routes defined in the root module. .forRoot()应该只为根模块中定义的路由调用。 For routes defined in any other routed modules you should call .forChild() .对于在任何其他路由模块中定义的路由,您应该调用.forChild()

So, keep the .forRoot() in the AppRoutingModule module only, and change .forRoot(authRoutes) to .forChild(authRoutes) in the AuthRoutingModule .因此,仅保留AppRoutingModule模块中的.forRoot() ,并将 AuthRoutingModule 中的.forRoot(authRoutes)更改为AuthRoutingModule .forChild(authRoutes) Do the same for the routes of libros and prestamos module.librosprestamos模块的路由执行相同的操作。

Not directly related to the error:与错误没有直接关系:
With your current route definition, to navigate to the AuthComponent you'll need the route /auth/auth .使用您当前的路由定义,要导航到AuthComponent ,您需要路由/auth/auth Consider defining the routes in AuthRoutingModule as -考虑将AuthRoutingModule中的路由定义为 -

const authRoutes: Routes = [  
    { path: 'login', component: LoginComponent},
    { path: 'forgotPassword', component: ForgotPasswordComponent},
    { path: 'signUp', component: SignUpComponent},
    { path: '', component: AuthComponent},
];

This will make 'auth' the default route for the auth module and display the AuthComponent with the route /auth .这将使'auth'成为 auth 模块的默认路由,并显示带有路由/authAuthComponent Follow similar approach for the other two lazy modules.对其他两个惰性模块遵循类似的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM