简体   繁体   English

什么是Mixin?何时在聚合物中使用它?

[英]What is Mixin and when to use it in the polymer?

I am quite new to Polymer framework and was checking if we can inherit from multiple classes in it, then I came across idea of Mixin. 我对Polymer框架很陌生,正在检查是否可以从其中的多个类继承,然后我想到了Mixin的想法。 Still I have some confusions about it. 我仍然对此有些困惑。 I need good examples explaining idea of Composition, Multiple inheritance and role of Mixin to achieve it. 我需要一些很好的示例来说明“合成”,“多重继承”和Mixin的作用这一概念。 I shall be grateful for the help. 我将感谢您的帮助。

Thanks 谢谢

Mixins 混合蛋白

A mixin is an abstract subclass; mixin是一个抽象子类; ie a subclass definition that may be applied to different superclasses to create a related family of modified classes. 即,可以应用于不同超类以创建相关的修改类族的子类定义。 - Gilad Bracha and William Cook, Mixin-based Inheritance -Gilad Bracha和William Cook,基于Mixin的继承

In simpler words a class expression mixins let you share code between elements without adding a common superclass. 用简单的话来说,类表达式mixins使您无需添加公共超类即可在元素之间共享代码。 In Polymer a mixin looks similar to a typical Polymer prototype and can define lifecycle callbacks, declared properties, default attributes, observers, and event listeners 在Polymer中,mixin看起来与典型的Polymer原型相似,并且可以定义生命周期回调,声明的属性,默认属性,观察者和事件侦听器

Here is a simple example 这是一个简单的例子

SelectedMixin = function (superClass) {
return class extends superClass {

    static get properties() {
        return {
            isSelected: {
                type: Boolean,
                value: false,
                notify: true,
                observer: '_selectedChanged'
            }
        };
    }

    _selectedChanged(selected) { 
        // do something
    }

    getSelectedItems() {
        // do something
    }
   }
}


class MyElement1 extends SelectedMixin(Polymer.Element) { 

      hightligh(){
            const selected = this.getSelectedItems();
            // do something
      }
}

class MyElement2 extends SelectedMixin(Polymer.Element) { 

    sort(){
        const selected = this.getSelectedItems();
        // do something
    }
}

You can see that we reused the SelectedMixin with two elements without the need to duplicate the code in the two elements. 您可以看到,我们在两个元素之间重用了SelectedMixin ,而无需在两个元素中重复代码。 If you have more than one mixin then you can use them like this 如果您有多个mixin,则可以像这样使用它们

class MyElement extends MixinB(MixinA(Polymer.Element)) { ... }

You can read more about mixins from here 您可以从这里阅读更多关于mixin的信息

Polymer 2 supports also behaviors which work in a similar way to mixins. 聚合物2还支持行为,其行为与混合素相似。

Behaviors 行为举止

Polymer 1 supported extending custom element prototypes with shared code modules called behaviors. Polymer 1支持使用称为行为的共享代码模块扩展自定义元素原型。

A simple example for behaviors can be written like this 可以这样写一个简单的行为示例

<script>
SelectedBehavior = {

  properties: {
    isSelected: {
      type: Boolean,
      value: false,
      notify: true,
      observer: '_selectedChanged'
    }
  },

  _selectedChanged: function(selected) {
      // do something 
  },
};
<script>

<script>
  Polymer({
     is: 'my-element',
     behaviors: [SelectedBehavior]
  });
<script>

In Polymer 2 these behaviors cannot be used anymore like this. 在聚合物2中,不能再像这样使用这些行为。 instead, you use them like this 相反,您可以像这样使用它们

<script>

  class MyElement extends Polymer.mixinBehaviors(
     [SelectedBehavior] , Polymer.Element) 
  {
      static get is() { return 'my-element' }
  }
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM