加载子组件选择器时加载父组件(没有层次结构URL)?

时间:2019-02-14 09:34:48

标签: angular router angular7 router-outlet

我想在使用选择器加载子组件时加载父组件。

示例:

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模板。

在其他组件中使用时,如何使用加载了父对象的选择器来加载子组件?

0 个答案:

没有答案