使用Polymer 2.x语法创建扩展另一个类(从Polymer.Element扩展)的类,是否可以在超类中定义一组默认属性,可以在子类中重写?
我的尝试是:
class ParentClass extends Polymer.Element
static get properties() {
return {
myProperty: {
type: String,
value: "x"
}
}
}
}
class ChildClass extends ParentClass {
static get properties() {
return {
myProperty2: String
}
}
}
当创建ParentClass时,期望是myProperty ==“x”,并且当创建ChildClass以使myProperty ==“x”并且具有myProperty2的附加属性时。实际上,ChildClass只有myProperty2。
也许ChildClass应该调用超类属性并合并属性,但我找不到调用超类的方法,因为它被声明为static(?)。
答案 0 :(得分:0)
您需要在parent-class
元素中导入child-class
。然后,要访问parent-class
的属性,只需在child-class
上调用该属性,例如:this.myProperty
。
您还可以覆盖child-class
中的属性值。
以下是plnkr中的演示:http://plnkr.co/edit/7kkitdcCZkbySNClxgTo?p=preview。
注意:子类(子类)的属性和观察者是 添加到超类(父类)定义的那些。
子类也可以从其超类继承模板。