【发布时间】:2016-04-13 02:47:03
【问题描述】:
我希望能够创建一个类以传递给 Polymer 函数以创建元素。一个明显的用例是拥有一个我们的开发人员可以用来构建 Polymer 元素的基类。
然而,Polymer 似乎忽略了类的生命周期方法。以下代码不运行#created。有解决办法吗?
//attempt 1
class CustomElement {
is = 'sample-multi-view-polymer-buic'
created() { console.log('created') } // never called
}
export default Polymer(new CustomElement()) // doesn't work (see error below)
由于我使用 Babel 将 ES6 转译为 ES5,所以上面的代码等价于下面的代码。它也不起作用。
// attempt2
function CustomElement() {
this.is = 'sample-multi-view-polymer-buic'
}
CustomElement.prototype.created= function() { console.log('hi') } //never called
【问题讨论】:
标签: class ecmascript-6 polymer-1.0