如何覆盖计算属性?
这是一个片段,其中父类A
计算result
属性等于value
属性,而其子类B
将重新< / strong>将result
属性计算为等于value + value2
。
但覆盖不起作用......
class A extends Polymer.Element {
static get properties(){
return {
value : Number,
result : {
type : Number,
computed : "computeProperty(value)"
}
}
}
computeProperty(value){
return value;
}
}
class B extends A {
static get is() { return "b-element"; }
static get template(){
return `
<div>Value : [[value]]</div>
<div>Value2 : [[value2]]</div>
<div>Result : [[result]]</div>
`
}
static get properties(){
return {
value2 : Number,
result : {
type : Number,
computed : "recomputeProperty(value, value2)"
}
}
}
recomputeProperty(value, value2){
return value + value2;
}
}
// Register the new element with the browser
customElements.define(B.is, B);
<script src="https://polygit.org/components/webcomponentsjs/webcomponents-loader.js"></script>
<link rel="import" href="https://polygit.org/components/polymer/polymer-element.html">
<b-element value="1" value2="2">
</b-element>