【发布时间】:2017-03-23 20:17:57
【问题描述】:
我正在尝试通过继承一些具有一些 Input 属性的基类来实现 Angular 2 中的组件(我使用 this example 作为实现 Angular 2 组件继承的指南)。
简化示例
export class ComponentBase {
@Input() text: string = "base";
}
@Component({
selector: "derived-comp",
templateUrl: "derived-comp.template.html",
providers: [
{ provide: ComponentBase, useExisting: forwardRef(() => DerivedComponent) }
]
})
export class DerivedComponent extends ComponentBase {
@Input() dummy: number;
}
这一切都按预期工作,直到我在派生组件中添加一个新的 Input 属性(虚拟,在提供的示例中)。当我这样做时,基类中定义的所有 Input 属性在运行时都变得不可见(尽管在编译时一切正常),并且浏览器控制台中出现错误说这些输入不存在:
无法绑定到“文本”,因为它不是 '派生补偿'
有人在 Angular 2 中遇到过类似的组件继承问题吗?有没有人有关于如何克服它的建议(除了复制粘贴代码)?
【问题讨论】:
-
谢谢 yurzui!我看过 Thierry Templier 的帖子,但当时似乎没有仔细阅读。遗憾的是,为了使用 ang2 组件的继承,您必须使用反射和一些“技巧”...
标签: javascript angular typescript inheritance