【发布时间】:2018-07-03 12:20:23
【问题描述】:
我需要从 HTMLTextAreaElement 扩展一个自定义元素,以便在表单中使用并直接获取值。但我总是收到非法构造函数消息
HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div>
<working-element></working-element>
</div>
<div>
<crashing-element></crashing-element>
</div>
</body>
<script src="myScript.js">
</html>
Typescript(编译为 ES6 到 myScript.js):
// all works fine
class newElementWorks extends HTMLElement {
constructor(){
super();
console.log('newElementWorks');
}
}
customElements.define('working-element', newElementWorks);
// this one crashes on super() call
class newElementCrash extends HTMLTextAreaElement {
constructor(){
super();
console.log('newElementCrash');
}
}
customElements.define('crashing-element', newElementCrash);
脚本在支持 ES6 和 Custom-Element 的 Chrome 版本 63.0.3239.132 上执行
我已经尝试包含 webcomponents/custom-elements polyfill
你知道为什么从 HTMLElement 以外的地方扩展会崩溃吗?
【问题讨论】:
-
你会将 TypeScript 代码转换成 ES5 还是 ES6?
-
@Bergi : 到 ES6
标签: javascript html typescript ecmascript-6 custom-element