如何覆盖 Polymer 2 中的组件功能?

How to override a component's function in Polymer 2?

我正在使用 app-localstorage-document 在浏览器中存储数据。文档说要覆盖 zeroValue 方法来定义在没有存储数据时使用的默认值。但是我不知道如何在 Polymer 中重写组件的方法。这是我尝试过的方法,但我认为它不正确,因为未调用该函数。

<app-localstorage-document key="CatValue" data="{{cat}}"></app-localstorage-document>

class MyApp extends Polymer.Element{
   static get is(){return 'my-app';}
   static get properties(){
     return{
       cat:{
         type: String,
         value: ""
       }
     };
   }

   zeroValue(){
     this.set('cat',"a cat");
   }
}

这可能对您有帮助:

<app-localstorage-document key="CatValue" data="{{cat}}"></app-localstorage-document>

class MyApp extends Polymer.Element{
   static get is(){return 'my-app';}
   static get properties(){
     return{
       cat:{
         type: String,
         value: function() {
              return this.zeroValue;
         }
       }
     };
   }

   //Override the default method
   get zeroValue(){
     return 'a cat';
   }
}

如果您不想覆盖 zeroValue(),它会将键 CatValue 的值存储为未定义。