我想在使用选择器加载子组件时加载父组件。
示例:
comp-parent.ts
@Component({
selector: 'comp-parent',
template: `
<p>parent works</p>
<router-outlet></router-outlet>
`,
styleUrls: ['./comp-parent.css']
})
export class ComponentParent {
protected foo: string;
}
comp-children.ts
@Component({
selector: 'comp-children',
template: "<p>Children works</p>",
styleUrls: ['./comp-children.css']
})
export class ComponentChildren extends ComponentParent { }
routing.ts
const routes: Routes = [{
path: '',
component: ComponentParent,
children: [{
path: '',
component: ComponentChildren
}]
}
];
@NgModule({
imports: [RouterModule.forChild(routes)],
exports: [RouterModule]
})
export class ComponentRoutingModule {}
component-foo-bar.ts
@Component({
selector: 'component-foo-bar',
templateUrl: "<comp-children>",
styleUrls: ['./component-bar.css']
})
export class ComponentFooBar { }
在ComponentBar
中,我希望comp-children
将是comp-parent
,其中包含comp-children
,但是仅加载comp-children
模板。
在其他组件中使用时,如何使用加载了父对象的选择器来加载子组件?