简体   繁体   English

具有扩展模板的组件子选择器

[英]Component child selector with extended template

I want to create component with view hierarchy outside of Router and RouterModule 我想创建具有Router和RouterModule之外的视图层次结构的组件

Example: 例:

comp-parent.ts 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 COMP-children.ts

@Component({
  selector: 'comp-children',
  template: "<p>Children works</p>",
  styleUrls: ['./comp-children.css']
  })
export class ComponentChildren extends ComponentParent { }

component-foo-bar.ts 组件FOO-bar.ts

@Component({
  selector: 'component-foo-bar',
  template: "<comp-children></comp-children>",
  styleUrls: ['./component-bar.css']
  })
export class ComponentFooBar { }

In ComponentBar I would expect that comp-children will be comp-parent with comp-children included, but only comp-children template is loaded. ComponentBar我希望comp-children将是comp-parent包括comp-children ,但是仅加载comp-children模板。

How to load parent template with <router-outlet/> resolved to comp-children template? 如何使用解析为comp-children模板的<router-outlet/>加载父模板?

Just replace the 只需更换

@Component({
  selector: 'component-foo-bar',
  template: "<comp-children>",
  styleUrls: ['./component-bar.css']
  })
export class ComponentFooBar { }

with

@Component({
  selector: 'component-foo-bar',
  template: "<comp-children></comp-children>",
  styleUrls: ['./component-bar.css']
  })
export class ComponentFooBar { }

When you inherit a class, only you can use the logic of the base class but not the template of it. 当您继承一个类时,只有您可以使用基类的逻辑,而不能使用其模板。

Infact, you have not used the child component inside the parent component. 实际上,您尚未在父组件中使用子组件。 ( I meant using child component selector in parent component template) (我的意思是在父组件模板中使用子组件选择器)

So they are not parent and child actually and they are just base and derived classes 因此,它们实际上不是父级和子级,它们只是基类和派生类

If you want to make such hierarchy you don't need to use router-outlet nor inheritance. 如果要建立这样的层次结构,则无需使用router-outlet或继承。 Just use components directly (by selectors). 只需直接使用组件(通过选择器)即可。 See StackBlitz demo. 请参阅StackBlitz演示。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM