简体   繁体   English

NgModule安装自己的路由

[英]NgModule installing its own routes

I am trying to figure out how to empoy @NgModule to better organize an Angular 2 modular app. 我试图找出如何empoy @NgModule更好地组织一个角2模块化的应用程序。 In particular, I am interested in an NgModule installing its own routes into the app. 特别是,我对NgModule 自己的路由安装到应用程序中感兴趣。 Does anyone have a working example showing how to go about it? 有没有人有一个可行的例子来说明如何去做?

Let's say for example you have the home module. 假设您有home模块。

-home
 -- home.component.ts
 -- home.component.html
 -- home.component.spec.ts
 -- home.routes.ts

// home.routes.ts // home.routes.ts

import { Routes, RouterModule } from "@angular/router";
import { HomeComponent } from "./home.component";
const routes : Routes = [
  {
    path: '',
    component: HomeComponent
  }
]

export default RouterModule.forChild(routes)

Then in your AppModule top level routes: 然后在您的AppModule顶级路线中:

const routes : Routes = [
  {
    path: '',
    loadChildren: 'app/modules/home/home.module',
    pathMatch: 'full'
  }
]

@NgModule({
  imports: [RouterModule.forRoot(routes)],
  export class AppModule {
    constructor() {
   }
  }

With this your home module will be lazy loading. 有了这个,你的家庭模块将被延迟加载。

暂无
暂无

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

相关问题 如何在Angular2中添加ngModule路由作为另一个ngModule的子路由? - How to add ngModule routes as child routes of another ngModule in Angular2? 在 Angular 中基于条件加载 ngModule 及其组件 - Load ngModule and its components based on conditional in Angular 如何忽略导入到定义自己路由的父功能模块中的功能模块的路由 - How do I ignore the routes of feature modules imported into a parent feature module which defines its own route 在 app.routing.module.ts 的 ngModule 中动态分配路由 - Dynamically Assign Routes in ngModule of app.routing.module.ts 在角度延迟加载路由上找不到“未定义”的 NgModule 元数据 - No NgModule metadata found for 'undefined' on angular lazy loading routes 用自己的路由延迟加载ngModules - Lazy Loading ngModules with own routes NgModule,@ NgModule,AppModule - NgModule, @NgModule, AppModule angular - 安装 angular-resource 并在 @NgModule 中导入后抛出 $$minErr 未定义错误 - angular - After installing angular-resource and importing in @NgModule throws $$minErr undefined error 在Angular4中找不到“ AppModule”的NgModule元数据,我无法在main.ts中找到其显示原因 - No NgModule metadata found for 'AppModule' in Angular4 i am not able to find the reason its showing in main.ts NgModule路由带有2.0.0-rc.5的Angular2时显示错误未处理的承诺拒绝错误:无法匹配任何路由:''(…) - NgModule routing Angular2 with 2.0.0-rc.5 showing error Unhandled promise rejection Error: Cannot match any routes: ''(…)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM