0

I want to create component with view hierarchy outside of Router and RouterModule

Example:

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 { }

component-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.

How to load parent template with <router-outlet/> resolved to comp-children template?

Bardr
  • 2,319
  • 2
  • 12
  • 21
Mike Quoro
  • 141
  • 1
  • 7

3 Answers3

0

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 { }
Baruch Gans
  • 1,415
  • 1
  • 10
  • 21
0

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

dileepkumar jami
  • 2,185
  • 12
  • 15
0

If you want to make such hierarchy you don't need to use router-outlet nor inheritance. Just use components directly (by selectors). See StackBlitz demo.

Bardr
  • 2,319
  • 2
  • 12
  • 21
  • It's part of bigger mechanism, I can't use children in parent component like provided in your example because its not extending base template. It simply uses `children-component` inside `parent-component`. – Mike Quoro Feb 14 '19 at 15:15