在Polymer 1.0中,我有以下代码片段: -
CustomBehaviorImpl = {
properties: {
// Define property here...
// ...
},
// Other custom behaviors definition
// ...
};
CustomBehavior = [
Polymer.AppNetworkStatusBehavior,
CustomBehaviorImpl,
];
如何在Polymer 2.0中创建CustomMixin
类。
答案 0 :(得分:0)
如果将mixins分离到自己的文件中,则可以在编写使用mixins的元素时将它们作为Html Import依赖项引用。
只需使用自定义行为扩展Polymer类。
我认为这个问题已经在stackoverflow上得到了解答
看一下这个:
Applying Behaviors with JS Mixins in Polymer 2
示例强>
这是我的CustomMixin.html
<script>
// CustomMixin provides the _customMixin function
var CustomMixin = function(superClass) {
return class extends superClass {
_customMixin() {
}
}
}
</script>
这是我使用CustomMixin的Polymer Element。
<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../CustomMixin/CustomMixin.html">
<dom-module id="my-element">
<template>
<style>
:host {
display: block;
}
</style>
</template>
<script>
class MyElement extends CustomMixin(Polymer.Element) {
static get is() { return 'my-element'; }
static get properties() {
return {
};
}
ready() {
super.ready();
}
}
window.customElements.define(MyElement.is, MyElement);
</script>
</dom-module>
我实际上从未测试过该代码,但我相信这应该是实现自定义mixin的方法。