angular 2 从基础组件继承 [英] angular 2 inheriting from a base-component

查看:24
本文介绍了angular 2 从基础组件继承的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是这里另一个问题的扩展:Angular2 和类继承支持

My question is an extension to another question here on so: Angular2 and class inheritance support

这是我的plunkr:http://plnkr.co/edit/ihdAJuUcyOj5Ze93BwIQ?p=preview

And here is my plunckr: http://plnkr.co/edit/ihdAJuUcyOj5Ze93BwIQ?p=preview

我想要做的是以下内容:

What I am trying to do is the following:

我有一些通用功能,我的所有组件都必须使用这些功能.由于已经在上述问题中得到了回答,因此可以这样做.

I have some common functionallity whcih all of my components will have to use. As it already has been answered in the aforementioned question, this can be done.

我的问题是:我可以在基础组件中注入依赖项吗?在我的 plunkr 中,登录到控制台时未定义声明的依赖项 (FormBuilder).

My question is: Can I have dependencies injected in the base-component? In my plunkr the declared dependency (FormBuilder) is undefined when logged to console.

import {AfterContentChecked, Component, ContentChildren, Input, QueryList, forwardRef, provide, Inject} from '@angular/core';
import { FormGroup, FormControl, Validators, FormBuilder, REACTIVE_FORM_DIRECTIVES } from '@angular/forms';



@Component({
  providers: [FormBuilder]
})
export class BaseComponent {
  // Interesting stuff here
  @Input() id: string;

  constructor(formBuilder: FormBuilder){
    console.log(formBuilder);
    console.log('inside the constructor');
  }


}

@Component({
  selector: 'child-comp2',
  template: '<div>child component #2 ({{id}})</div>',
  providers: [provide(BaseComponent, { useExisting: forwardRef(() => ChildComponent2) })]
})
export class ChildComponent2 extends BaseComponent {


}

@Component({
  selector: 'child-comp1',
  template: '<div>child component #1 ({{id}})</div>',
  providers: [provide(BaseComponent, { useExisting: forwardRef(() => ChildComponent1) })]
})
export class ChildComponent1 extends BaseComponent {


}

@Component({
  selector: 'parent-comp',
  template: `<div>Hello World</div>
   <p>Number of Child Component 1 items: {{numComp1}}
   <p>Number of Child Component 2 items: {{numComp2}}
   <p>Number of Base Component items: {{numBase}}
   <p><ng-content></ng-content>
   <p>Base Components:</p>
   <ul>
    <li *ngFor="let c of contentBase">{{c.id}}</li>
   </ul>
  `
})
export class ParentComponent implements AfterContentChecked  {

  @ContentChildren(ChildComponent1) contentChild1: QueryList<ChildComponent1>
  @ContentChildren(ChildComponent2) contentChild2: QueryList<ChildComponent2>
  @ContentChildren(BaseComponent) contentBase: QueryList<BaseComponent>
  public numComp1:number
  public numComp2:number
  public numBase:number

  ngAfterContentChecked() {
    this.numComp1 = this.contentChild1.length
    this.numComp2 = this.contentChild2.length
    this.numBase = this.contentBase.length
  }
}

@Component({
  selector: 'my-app',
  template: `<parent-comp>
      <child-comp1 id="A"></child-comp1>
      <child-comp1 id="B"></child-comp1>
      <child-comp2 id="C"></child-comp2>
    </parent-comp>
  `,
  directives: [ParentComponent, ChildComponent1, ChildComponent2]
})
export class MyApplication  {

}

推荐答案

这是不可能的,因为 Angular2 只会查看当前组件的注释,而不查看上面组件的注释.

It's not possible the way you do since Angular2 will only have a look at the annotations for the current component but not on the component above.

话虽如此,您可以在注解层面进行工作,使父组件继承注解:

That being said, you can work at the level of annotations to make inherit annotations of the parent component:

export function Inherit(annotation: any) {
  return function (target: Function) {
    var parentTarget = Object.getPrototypeOf(target.prototype).constructor;
    var parentAnnotations = Reflect.getMetadata('design:paramtypes', parentTarget);

    Reflect.defineMetadata('design:paramtypes', parentAnnotations, target);
  }
}

像这样使用它:

@Inherit()
@Component({
  (...)
})
export class ChildComponent1 extends BaseComponent {
  constructor() {
    super(arguments);
  }
}

查看此问题了解更多详情:

See this question for more details:

您可以通过以下文章了解幕后发生的事情:

The following article could interest you to understand what happens under the hood:

您还需要注意,直接处理注释有一些缺点,尤其是在离线编译和 IDE 中的组件自省方面.

You also need to be aware that working on annotations directly has drawbacks especially regarding offline compilation and for component introspection in IDEs.

这篇关于angular 2 从基础组件继承的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆