将参数传递给类

时间:2017-04-02 10:51:01

标签: javascript ecmascript-6 polymer es6-class polymer-2.x

我正在尝试为Polymer 2.0组件(基于类的语法)创建一个mixin,如下所示。有没有办法按照我现在的方式将选项传递给类?

使用mixin进行聚合物元素定义:

class PodcastListView extends PolymerApolloBehavior(Polymer.Element, myOptions) {
   //some code 
}

mixin类:

export const PolymerApolloBehavior = (superclass, options) => class extends superclass {
    constructor() {
       console.log(options);
    }
}

1 个答案:

答案 0 :(得分:1)

我可以为你的PodcastListView类建议你的函数包装器,它将创建一个具有新行为的新类。它看起来像这样:

export const PolymerApolloBehavior = (original, options) => {
  let result = Object.assign({}, original)
  const originalConstructor = result.prototype.constructor
  result.prototype.constructor = () => {
    console.log(options) // your behavior
    originalConstructor() // original constructor
  }
  // any changes of class
  return result
}

现在你可以应用你的新行为了:

export default PolymerApolloBehavior(PodcastListView, {behaviorOptions})