typescript - 类继承不起作用
我正在尝试构建一个继承自基类属性的 Icon 组件。然而,当实例化 Icon 时,super 的属性都不存在,__proto__ 属性没有被设置。
我觉得我错过了一些非常明显的东西。我运行了一个非常简单的 CodePen 版本的代码,它的工作效果如预期:CodePen 示例
然而,将其引入到 Angular 2 的上下文中时,继承的属性总是 undefined。
BaseComponent.ts
export abstract class BaseComponent {
constructor() {
this.baseImagePath = '/Assets/Images';
}
baseImagePath: string;
}
图标.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
对这里进入兔子洞之旅表示抱歉。走了半小时的代码,不知何故,BaseComponent.ts 没有被编译。运行 tsc 几次后,一切突然开始正常工作。
感谢大家的帮助!