简体   繁体   English

继承的 angular 2 组件中的输入属性

[英]Input properties in inherited angular 2 components

I'm trying to implement components in angular 2 by inheriting some base class which has some Input properties (I've used this example as a guide for implementing inheritance for angular 2 components).我试图通过继承一些具有一些 Input 属性的基类来实现 angular 2 中的组件(我使用这个示例作为实现 angular 2 组件继承的指南)。

Simplified Example简化示例

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;
}

It all works as expected, until I add a new Input property in derived component (dummy, in the provided sample).这一切都按预期工作,直到我在派生组件中添加一个新的 Input 属性(在提供的示例中是虚拟的)。 When I do that, all of the Input properties defined in base class become invisible in runtime (although everything is ok at compile-time), and there's an error in browser console saying that those inputs don't exist:当我这样做时,基类中定义的所有 Input 属性在运行时都变得不可见(尽管在编译时一切正常),并且浏览器控制台中出现错误,指出这些输入不存在:

Can't bind to 'text' since it isn't a known property of 'derived-comp.'无法绑定到 'text',因为它不是 'derived-comp' 的已知属性。

Has anyone had a similar problem with component inheritance in angular 2?有没有人在 angular 2 中遇到过类似的组件继承问题? Does anyone have a suggestion on how to overcome it (other than copy-paste of the code)?有没有人有关于如何克服它的建议(除了代码的复制粘贴)?

Maybe in old Angular versions this was a pia, but now it's very easy.也许在旧的 Angular 版本中这是一个 pia,但现在它很容易。 See this post看到这个帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM