Polymer multiple inheritance/composition

杀马特。学长 韩版系。学妹 提交于 2019-12-19 08:05:11

问题


Polymer website says multiple inheritance (or composition) is not supported using 'extend' attribute in Polymer. I want an element to be composed of some methods from one Polymer element, and some others from another, to have it reflect application logic. Is there currently any way to implement that in Polymer? (like doing that using javascript mixins)


回答1:


Polymer now supports 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

More info here




回答2:


I can't speak to the reasoning of the Polymer folks, but it's generally considered preferable to use composition over inheritance.




回答3:


Polymer support Mixin concept for overcome the Multiple inheritance concept.

Example :

Class ElementOne extends Polymer.Element {
   ready() {
     super.ready();   
   }
}

Class ElementTwo extends Polymer.Element {
  ready() {
     super.ready();
  }
}

Class ElementThree extends ElementOne(ElementTwo(Polymer.Element)) {
  ready() {
     super.ready();
  }
}

I hope it's helpful to you.



来源:https://stackoverflow.com/questions/23659670/polymer-multiple-inheritance-composition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!