【问题标题】:How to Route to Feature Module如何路由到功能模块
【发布时间】:2018-09-12 06:40:56
【问题描述】:

我试图让来自一个模块的路由包含来自另一个模块的组件。我正在尝试实施的方案是:/group/feature,它应该显示 feature.html 中的内容,该内容包含在路由器插座所在的 group.html 的内容中。我可以让/group 工作,但/group/feature 返回page not found

完整的项目在这里:https://stackblitz.com/edit/angular-nsfgyr

这里是主要文件:

feature.module.ts:

@NgModule({
  imports: [
    CommonModule
  ],
  declarations: [FeatureComponent],
  exports: [FeatureComponent]
})
export class FeatureModule {
}

group-module.ts

@NgModule({
  imports: [
    CommonModule,
    FeatureModule,
    GroupRoutingModule,
  ],
  declarations: [
    GroupComponent,
  ],
  exports: [GroupComponent]
})
export class GroupModule {
}

group-routing.module.ts:

const ingestRoutes: Routes = [
  {path: 'feature', component: FeatureComponent}
];

@NgModule({
  imports: [
    RouterModule.forChild(ingestRoutes)
  ],
  exports: [
    RouterModule
  ]
})
export class GroupRoutingModule {
}

app-routing.module:

const routes: Routes = [
    {path: '', pathMatch: 'full', redirectTo: 'group'},
    {path: 'group', component: GroupComponent},
    {path: '**', component: PageNotFoundComponent}
  ]
;

@NgModule({
  imports: [
    RouterModule.forRoot(
      routes,
      {enableTracing: false, useHash: false}
    )
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {
}

【问题讨论】:

  • 看这里stackblitz.com/edit/angular-9f3ssu,更新了group-routing-module中的路由
  • 感谢@Deshak9,但是当我转到/group/feature 时,它只会呈现feature.html。我试图让它呈现 groupl.html 中的内容,其中 feature.html 包含在路由器插座中。这可能吗?

标签: angular


【解决方案1】:

您已经为'group' 定义了路由。你还没有定义路由'group/feature'

更新:

你的 group 路由看起来像一个普通的 /group 路由,但 group 实际上是一个子模块,你不加载组件,你 loadChildren

const routes: Routes = [
    {path: '', pathMatch: 'full', redirectTo: 'group'},
    {path: 'group', loadChildren: 'app/group/group.module#GroupModule},
    {path: '**', component: PageNotFoundComponent}
  ]
;

【讨论】:

  • group-routing.module.ts 上的feature 路由不会这样做吗?
  • @Brian。您应该能够根据您的代码转到“功能”。只需为每个模块包含一个 。实现延迟加载的方法有很多。
  • @Brian。以下链接不直接回答您的问题。但是当我路由到电子设备时,路径就是路径:“电子”,当我路由到“苹果”时,路径就是 path="apple"。 Electronic 和 Apple 有自己的路由模块,将自己的路由插座导入 appRouting 模块。我只是想帮助你。电子商务网站的类别/子类别或类别/项目的 Angular 4 路由选择。我会尽力回答你的问题。
  • 我在这里实施了您的建议:stackblitz.com/edit/angular-zmwgff?file=app/group/group.html。现在 /group/feature 渲染 feature.html 和 /group 什么都不渲染。我希望/group/feature 将内容包含在 group.html 中,并将 feature.html 的内容包含在 router-outlet 标记中。这可能吗?
  • 不确定。它们是独立模块中的组件。当我尝试在我的项目中的子模块 html 模板中添加父模块组件选择器时,我收到一个错误,即该组件不是模块的一部分。我可以在你的 stackblitz 链接中遇到同样的错误(谢谢你,顺便说一句。尝试了一些东西,但没有得到任何结果)。
【解决方案2】:

第一种方式。没有孩子,没有导入模块。

const routes: Routes = [
  {path: '', pathMatch: 'full', redirectTo: 'group'},
  {path: 'group/feature', component: FeatureComponent },
  {path: 'group', component: GroupComponent},
  {path: '**', component: PageNotFoundComponent}
];

@NgModule({
  imports: [
  RouterModule.forRoot(routes,
  {enableTracing: false, useHash: false}
  )
],
exports: [
  RouterModule
]
})
export class AppRoutingModule {
}

第二种方式是使用children..

const routes: Routes = [
  {path: '', pathMatch: 'full', redirectTo: 'group'},
  {path: 'group', component: GroupComponent,
   children: [
    { path: 'feature', component: FeatureComponent }]     
  },
  {path: '**', component: PageNotFoundComponent}
];

第三种方式是导入模块。它有自己的

"<router-outlet></router-outlet>".

const ingestRoutes: Routes = [
  { path: 'group/feature', component: FeatureComponent,
    children: [       
      path: 'more', component: MoreComponent, // if you want to add..
        // this will route to group/feature/more
      path: '', component: FeatureHomeComponent,
    ] 
  }
];

@NgModule({
  imports: [
    RouterModule.forChild(ingestRoutes)
  ],
  declarations: [
      FeatureComponent, FeatureHomeComponent, MoreComponent
  ],
  exports: [
    RouterModule
  ]
})
export class GroupRoutingModule {
}

FeatureComponent.html 只有

<route-outlet></router-outlet>. 

FeatureHomeComponent.html

- you can display anything here..YOUR ACTURE FEATURE COMPONENT.HTML

在 AppRoutingModule 中导出。

 const routes: Routes = [
    {path: '', pathMatch: 'full', redirectTo: 'group'},
    {path: 'group', component: GroupComponent},
    {path: '**', component: PageNotFoundComponent}
  ];

@NgModule({
  imports: [
    RouterModule.forRoot(routes, {enableTracing: false, useHash: 
    false}), GroupRoutingModule, // add this..
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {
}

我为“苹果品牌”所做的只是在我的 AppRoutingModule 中导入 AppleRoutingModule。见链接。Angular 4 route selection for category/subcategory or category/item for ecommerce web site

【讨论】:

  • 告诉我,我会尽力帮助您进一步路由。
  • 你孩子的方法正是我想要的——谢谢
  • 很高兴为您提供帮助。快乐编码。
【解决方案3】:

我想通了:在组路由器的子数组中添加特征路径有效:

在 group-routing.module.ts 中:

const ingestRoutes: Routes = [
  {path: 'group', component: GroupComponent,
   children: [
     {path: 'feature', component: FeatureComponent}
  ]},
];

感谢大家的回复和帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-28
    • 2018-12-27
    • 2017-04-11
    • 2013-05-06
    • 2018-11-02
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    相关资源
    最近更新 更多