我是 Angular 的新手。我有以下文件夹/文件树:
-- app folder
-- root (
-- pages (pages-routing, pages-module, pages-component)
-- page1 (page1-routing, page1-component)
-- subcomponent (subcomponent-component)
我的页面路由是这样定义的
const routes: Routes = [{
path: '',
component: PagesComponent,
children: [{
path: 'page1',
component: Page1Component,
}, {
path: 'subcomponent',
component: SubComponent,
}],
}];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule],
})
export class PagesRoutingModule { }
export const routedComponents = [
Page1Component,
SubComponent,
];
页面 NgModule
@NgModule({
imports: [
ThemeModule,
PagesRoutingModule,
],
declarations: [
...routedComponents,
],
providers: [
SmartTableService,
],
})
Page1 NgModule 看起来很相似:
@NgModule({
imports: [
ThemeModule,
PagesRoutingModule,
Ng2SmartTableModule,
],
declarations: [
...routedComponents,
],
providers: [
SmartTableService,
],
})
这可行,但我的问题是..我想知道是否可以删除子组件的路线..因为子组件不需要真正的路线?到目前为止我所做的是尝试从页面路由中删除
{
path: 'subcomponent',
component: SubComponent,
}
和
{
SubComponent,
}
我在 Pages NgModule 和 Page1 NgModule Declarations 部分都添加了 SubComponent 引用,但我得到:没有为 SubComponent 找到组件工厂
所以真正的问题。是否必须为组件提供路线?组件可以在没有路由的情况下生存吗?