混合行为
在2.0-preview branch of <iron-resizable-behavior> 中,Polymer.IronResizableBehavior 是一种混合行为(即,定义为对象而不是类混合)。 Polymer 2 提供Polymer.mixinBehaviors() 将一个或多个混合mixin 与一个类合并。
用法:
class NEW_CLASSNAME extends Polymer.mixinBehaviors(HYBRID_MIXINS_ARRAY, SUPERCLASSNAME) { ... }
例子:
class MyView1 extends Polymer.mixinBehaviors([Polymer.IronResizableBehavior], Polymer.Element) {
static get is() { return 'my-view1'; }
connectedCallback() {
super.connectedCallback();
this.addEventListener('iron-resize', this._logResizeEvent);
}
disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener('iron-resize', this._logResizeEvent);
}
_logResizeEvent(e) {
console.debug('resize', e);
}
}
window.customElements.define(MyView1.is, MyView1);
行为类混合
你可以像这样create a behavior-class mixin:
const MyMixin = (superclass) => class extends superclass {
foo() {
console.log('foo from MyMixin');
}
};
然后,您可以像这样在 Polymer 元素中使用它:
class MyView1 extends MyMixin(Polmer.Element) {
onClick() {
this.foo(); // <-- from MyMixin
}
}
混合行为+行为类混合
您可以像这样一起使用混合行为和行为类混合:
class MyView1 extends Polymer.mixinBehaviors([Polymer.IronResizableBehavior], MyMixin(Polmer.Element)) {
onClick() {
this.foo(); // <-- from MyMixin
console.log(this._interestedResizables); // <-- from Polymer.IronResizableBehavior
}
}