angular - 具有相同父布局 Angular 功能模块路由

标签 angular typescript angular2-routing

我想为不同的功能模块使用相同的布局(在 app.module.ts 中定义),每个模块都有自己的路由。还有一个单独的登录/注册布局,没有侧边菜单、页眉和页脚。到目前为止我试过这个:

//../app/app.component.html
<router-outlet></router-outlet> // here i want login and layout view.

和一个布局组件:

//../app/layout.component.html
<header></header>
<side-menu></side-menu>
<router-outlet></router-outlet> // here i want layout views that would have side menu with them e.g. dashboard, inventory etc
<footer></footer>

仪表板路线在 app.module.ts 中,但库存和其他模块有自己的模块和路线,如下所示:

//app.module.ts
const appRoutes: Routes = [
    { path: 'login', component: LoginComponent },
    {
        path: '',
        component: LayoutComponent,
        children: [
            { path: '', component: DashboardOne},
            { path: 'dashboardOne', component: DashboardOne},
            { path: 'dashboardTwo', component: DashboardTwo}
        ],
        canActivate: [AuthGuard]
    }
];    
@NgModule({
  declarations: [
    AppComponent,
    DashboardOneComponent,
    DashboardTwoComponent,
    LoginComponent,
    LayoutComponent
  ],
  imports: [
    RouterModule.forRoot(
      appRoutes,
      { enableTracing: true } // <-- debugging purposes only
    ),
    InventoryModule,
    WarehouseModule,
    UserModule,
  ],
  providers: [AuthGuard],
  bootstrap: [AppComponent]
})
export class AppModule { }

和另一个模块:

//inventory.module.ts
const appRoutes: Routes = [
    {
        path: 'inventory',
        //component: LayoutComponent,
        children: [
            { path: '', component: InventoryOne},
            { path: 'inventoryOne', component: InventoryOne},
            { path: 'inventoryTwo', component: InventoryTwo}
        ],
        canActivate: [AuthGuard]
    }
];
@NgModule({
  declarations: [
    AppComponent,
    InventoryOneComponent,
    InventoryTwoComponent,
    //LayoutComponent // want to use this layout in other modules too
  ],
  imports: [
    RouterModule.forChild(
      appRoutes
    ),
  ],
  providers: [],
})
export class InventoryModule { }

如果我从库存模块中删除布局组件的注释,它会重新呈现布局组件(我不想要那样)我想在每个有自己的路由但到目前为止无法这样做的模块中使用 layoutComponent。

最佳答案

在您的 AppModule 中,您可以默认重定向到 FeaturesModule,它将具有来自 LayoutComponent 的菜单,而 AuthGuard 可以重定向需要时到 /login

const appRoutes: Routes = [
  {        
    path: '',
    loadChildren: '../<insertyourpath>/features.module#FeaturesModule',
    canActivate: [AuthGuard]
  },
  {
     path: 'login',
     component: LoginComponent
  }
];

在您的 FeaturesModule 中,您将在 LayoutComponent 的导出中呈现特征路径:

const featureRoutes: Routes = [
  {
    path: '',
    component: LayoutComponent,
    children: [
      {        
        path: 'inventory',
        loadChildren: '../<insertyourpath>/inventory.module#InventoryModule'
      }
    ]
  }
];

在您的 InventoryModule 中,您放置了所有模块的子路由(分别为其他 FeatureModule)。仪表板必须移动到 FeaturesModule 或它自己的模块。:

const inventoryRoutes: [
  { path: '', component: InventoryOne},
  { path: 'inventoryOne', component: InventoryOne},
  { path: 'inventoryTwo', component: InventoryTwo}
];

请注意,使用给定的 loadChildren 语法,引用的模块将被延迟加载。如果您希望同步加载它,请查看此 SO answer .

关于angular - 具有相同父布局 Angular 功能模块路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48250269/

相关文章:

javascript - 从 Angular 指令访问 img 标签中的 "src"属性

javascript - 使用 Jasmine 和 Karma 进行 Angular Testing - 对于延迟加载的组件,路由器导出错误和组件创建测试失败

angularjs - share() vs ReplaySubject : Which one, 都不起作用

typescript - Angular 2 中的 RouteData 可以将变量从父组件传递给路由组件吗?

reactjs - 在 typescript 中嵌套连接的 redux 容器和演示组件

javascript - 设置 routerLink 参数 url 的格式

angular - 如何使用snackbar处理Angular中的错误

javascript - 在 angular2 路由应用程序中单击后退按钮时会发生什么?

angular - 设置 activatedRoute 可选参数 "id"完全不导航

angular - i18n 多方向 RTL Angular