How to get Polymer 2.0 ES5 elements working with v1 spec?

情到浓时终转凉″ 提交于 2019-12-14 03:52:48

问题


I've successfully built a number of Polymer 2.0 elements and they run great in ES6 capable browsers.

When I try to transpile them to ES5, the browser throws a bunch of errors, like these:

Failed to construct 'HTMLElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

After some research, it looks like you need a native-shim, which comes along with the webcomponentsjs-es5-loader, but after switching to that now I'm getting a new error:

Class constructor DomModule cannot be invoked without 'new'

How do transpiled Polymer 2.0 elements get used in a ES5 browser? Am I missing something?


回答1:


To use The Custom Element V1 Spec you must use classes. But ES5 browsers don't support classes. So there is an adapter that allows an ES5 browser to act like it is using classes.

See: https://github.com/webcomponents/webcomponentsjs#custom-elements-es5-adapterjs for more information.

You need to include the adapter on your ES5 browser before defining your Custom Element. This goes for Native components as well as Polymer 2+ components.



来源:https://stackoverflow.com/questions/43002652/how-to-get-polymer-2-0-es5-elements-working-with-v1-spec

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!