我需要从HTMLTextAreaElement扩展自定义元素,以便在表单中使用并直接获取值。但我总是得到Illegal Constructor Message
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崩溃以外的地方进行扩展吗?
答案 0 :(得分:5)
您看到的错误意味着您正在调用new
的对象没有[[construct]]
内部方法。
虽然规范表明您可以extend
HTML *元素类,但目前似乎不支持它(请参阅类似问题:https://github.com/webcomponents/custom-elements/issues/6),因此您只能扩展HTMLElement
此时此刻。
答案 1 :(得分:0)
在尝试扩展HTMLTextAreaElement
时,有两件事显然是错误的。
{ extends: 'textarea' }
第三个参数。is=""
属性中添加您的姓名。
class MyTextarea extends HTMLTextAreaElement {
constructor(){
super();
console.log('MyTextarea');
}
}
customElements.define('my-textarea', MyTextarea, {extends: 'textarea'});
<textarea is="my-textarea"></textarea>