代码之家  ›  专栏  ›  技术社区  ›  Arnaud Denoyelle

用routermodule解决循环依赖

  •  1
  • Arnaud Denoyelle  · 技术社区  · 7 年前

    official styleguide

    ├── Home page
    ├── Profile page
    │   ├── Preferences
    │   ├── Change password
    

    我想用一个“核心”模块、一个“共享”模块和一个“路由”模块来构建应用程序,并为应用程序的每个部分(本例中的“主页”和“配置文件”)提供一个模块,如下所示:

    ├── CoreModule
    ├── SharedModule
    ├── RoutingModule
    ├── HomeModule
    │   ├── Home Component
    ├── ProfileModule
    │   ├── ProfileComponent
    │   ├── PreferencesComponent
    │   ├── ChangePasswordComponent
    

    以下内容:

    1. RoutingModule ProfileComponent ProfileModule

    2. <a [routerLink]="..."> 需要导入

    我有循环依赖

    编辑 good example NgModules

    1 回复  |  直到 7 年前
        1
  •  2
  •   William Moore    7 年前

    你不应该进口你的 AppRoutingModule <a [routerLink]="..."> <A[路由器链接]=“…”> RouterModule app.module.ts

    应该看起来像:

    import { BrowserModule } from '@angular/platform-browser';
    import { NgModule } from '@angular/core';
    import { RouterModule } from '@angular/router';
    
    import { AppRouterModule } from './app-router.module';
    
    // Other imports including ProfileComponent
    
    @NgModule({
      declarations: [
        // various including ProfileComponent
      ],
      imports: [
        BrowserModule,
        RouterModule
      ],
      providers: [
      ],
      bootstrap: [
        AppComponent
      ]
    })
    export class AppModule { }
    

    app-router.module.ts

    import { NgModule } from '@angular/core';
    import { Routes, RouterModule } from '@angular/router';
    
    import { ProfileComponent } from '..';
    
    const routes: Routes = [
      {
        path: 'profile-component-url',
        component: ProfileComponent
      },
      // Other routes
    ];
    
    @NgModule({
      imports: [RouterModule.forRoot(routes)],
      exports: [RouterModule]
    })
    export class AppRouterModule { }
    

    ProfileComponent

    <a routerLink='/profile-component-url'>
    

    因此,没有循环依赖作为您的

    如果你想从懒惰的加载中获益,你需要另一个路由器模块,它将被使用。 RouterModule.forChild(routes) RouterModule.forRoot(routes) 它引用了惰性加载模块中的所有组件等,您需要在主路由器模块中引用它,如下所示:

      {
        path: 'some-path',
        loadChildren: './modules/..path-to-other-module../lazy-loaded.module#LazyLoadedModule'
      },
    
    推荐文章