我将 Polymer 应用程序转换为 Polymer 2。我正在将组件更改为 ES6 类语法(是的,我知道我可以将它们保留为 v1.7 混合样式,但我希望它们成为 ES6 类)。
但是,当我将代码转译回 ES5(使用 BabelJS)时,我遇到了一个关于 ES5“类”扩展本机元素的已知问题(https://github.com/babel/babel/issues/4480)。
我尝试了 babel-plugin-transform-custom-element-classes 但没有奏效。 所以我尝试了 webcomponents shim 来解决这个问题: https ://github.com/webcomponents/webcomponentsjs#custom-elements-es5-adapterjs
但是垫片不起作用!我不知道我做错了什么:(
<script src="webcomponentsjs/custom-elements-es5-adapter.js"></script>
<script src="webcomponentsjs/webcomponents-lite.js"></script>
...
<y-example></y-example>
...
<script>
/* transpiled to ES5 */
class YExample extends HTMLElement {}
customElements.define('y-example', YExample);
</script>
这是我的 jsbin: http ://jsbin.com/feqoniz/edit?html,js,output
请注意,我包含了 custom-elements-es5-adapter.js,还请注意 JS 面板使用的是 ES6/Babel。
如果您删除 custom-elements-es5-adapter.js 并将 JS 面板更改为普通 Javascript(不是 ES6/Babel),那么一切正常。
您可以包含或删除适配器(离开 ES6/Babel),错误基本上是一样的,除了当包含适配器时,它来自适配器代码:Failed to construction 'HTMLElement': Please use the 'new'运算符,这个 DOM 对象构造函数不能作为函数调用。
我一定是在做傻事?有任何想法吗?