聚合物多重遗传/组成

sep*_*ans 5 javascript inheritance web-component polymer

Polymer网站表示使用Polymer中的'extend'属性不支持多重继承(或组合).我希望一个元素由一个Polymer元素中的某些方法组成,而另一些元素来自另一个,以使它反映应用程序逻辑.目前有没有办法在Polymer中实现它?(就像使用javascript mixins那样)

sep*_*ans 7

Polymer现在支持mixin:

var mixinObj = {
   foo: function() {
     /* ... */
   }
};

var mixinObj2 = {
   foo2: function() {
     /* ... */
   }
};


Polymer('my-component', Polymer.mixin({   // Platform.mixin for polymer version < 0.5
  bar: function() {

     /* ... */
     this.foo();   // all the functions in mixinObjs are now accessible through 'this'   
     this.foo2();   



  }


}, mixinObj, mixObj2);  // Platform.mixin accepts multiple mixin objects
Run Code Online (Sandbox Code Playgroud)

更多信息在这里