类继承不起作用

时间:2016-04-13 10:31:41

标签: typescript angular

我正在尝试构建一个从基类继承属性的Icon组件。但是,当实例化Icon时,super中没有任何属性存在,则未设置__proto__属性。

我觉得我错过了一些非常明显的东西。我运行了一个非常简单的代码的codepen版本,它按预期工作:CodePen Example

但是,将其置于Angular 2的上下文中,继承的属性始终为undefined

BaseComponent.ts

export abstract class BaseComponent {
  constructor() {
    this.baseImagePath = '/Assets/Images';
  }

  baseImagePath: string;
}

Icon.ts

import {Component, Input} from 'angular2/core';
import {BaseComponent} from '../baseComponent';

@Component({
  selector: 'lib-icon',
  templateUrl: 'app/components/icon/icon.tmpl.html'
})
export class Icon extends BaseComponent {
  @Input() icon: string;

  constructor() {
    super();
    this.baseImagePath += '/icons/';
  }


  getSrc() {
    return `${this.baseImagePath}${this.icon}`;
  }
}

我已尝试将this.baseImagePath += '/icons/'行移至ngOnInit以防万一 - 它始终未定义。

对我所缺少的任何见解都将非常感激。

1 个答案:

答案 0 :(得分:0)

在兔子洞里旅行的道歉。因为BaseComponent.ts没有被编译的原因,半小时的代码遍历。运行tsc几次,它突然开始工作。

感谢大家的帮助!