[英]Class constructor PolymerElement cannot be invoked without 'new'
昨天我的应用程序工作得很好但是当我现在做polymer serve -o
它打开应用程序并在控制台中打印此错误。
Class constructor PolymerElement cannot be invoked without 'new'
According to this post , this issue is cause because $ polymer serve compiles your code to es5 automatically. 根据这篇文章 ,这个问题是因为$ polymer服务器自动将你的代码编译成es5。 The --compile never flag stops $ polymer serve from doing this.
--compile never flag会阻止$聚合物 发挥作用 。
I had a similar Error the other day after moving my Polymer App to v.2. 在将Polymer App移至v.2后的另一天,我遇到了类似的错误。
This helped me: 这对我有所帮助:
To work around this, load custom-elements-es5-adapter.js before declaring new Custom Elements.
要解决此问题,请在声明新的自定义元素之前加载custom-elements-es5-adapter.js。
Since most projects need to support a wide range of browsers that don't necessary support ES6, it may make sense to compile your project to ES5.
由于大多数项目需要支持不需要支持ES6的各种浏览器,因此将项目编译为ES5可能是有意义的。 However, ES5-style custom element classes will not work with native Custom Elements because ES5-style classes cannot properly extend ES6 classes, like HTMLElement.
但是,ES5样式的自定义元素类不适用于本机自定义元素,因为ES5样式的类无法正确扩展ES6类,如HTMLElement。
I build my Polymer App as es5-bundled, and serve it to Android App using WebView. 我将我的Polymer App构建为es5-bundled,并使用WebView将其提供给Android App。 This problem often appears.
这个问题经常出现。
In your polymer.json
, add custom-elements-es5-adapter
to the excludes
array to stop it from being compiled to ES5. 在你
polymer.json
,添加custom-elements-es5-adapter
的excludes
阵列被编译到ES5停止。
"builds": [
{
"bundle": {
"stripComments": true,
"inlineCss": true,
"sourcemaps": false,
"excludes": [
"bower_components/webcomponentsjs/webcomponents-loader.js",
"bower_components/webcomponentsjs/custom-elements-es5-adapter.js"
]
},
"js": {
"compile": true,
"minify": true
},
"css": {
"minify": true
},
"html": {
"minify": true
},
"addServiceWorker": false
The problem occurs because Custom Elements v1 requires your code to use ES6 syntax. 出现此问题的原因是自定义元素v1要求您的代码使用ES6语法。 So make sure you don't transpile to anything lower, like ES5.
因此,请确保不要像ES5那样转向更低级别的东西。
For anyone running into this using the Parcel bundler like me; 对于像我这样使用Parcel捆绑包的人来说; by default it compiles your code to ES5 or something, even if you're using Typescript and you've set the tsconfig target to ES6.
默认情况下,它会将您的代码编译为ES5或其他内容,即使您正在使用Typescript并且已将tsconfig目标设置为ES6。
The solution is to tell Parcel what browsers you're targeting, so that it knows it doesn't have to transpile to ES5. 解决方案是告诉Parcel您要定位的浏览器,以便它知道它不必转换为ES5。 One way to do it is to add a "browserlist" field in package.json.
一种方法是在package.json中添加“browserlist”字段。
I found out about this through this video . 我通过这个视频发现了这个。 For more info I suggest you go watch that.
有关更多信息,我建议你去看看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.