代码之家  ›  专栏  ›  技术社区  ›  Amanshu Kataria

如何覆盖聚合物2中组件的功能?

  •  1
  • Amanshu Kataria  · 技术社区  · 9 年前

    我正在使用 app-localstorage-document 在浏览器中存储数据。文件上说要覆盖 zeroValue 方法定义在没有存储数据时使用的默认值。但我不知道如何在聚合物中替代成分的方法。这是我尝试过的,但我认为它不正确,因为函数没有被调用。

    <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");
       }
    }
    
    1 回复  |  直到 9 年前
        1
  •  1
  •   Ofisora    9 年前

    这可能会帮助您:

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