如何创建一个扩展类的自定义元素的新实例 [英] How to create new instance of an extended class of custom elements
问题描述
我正在尝试 Google开发人员网站我收到错误:TypeError:非法构造函数
出了什么问题,如何解决?
I'm trying the example from google developer site and I'm getting Error: "TypeError: Illegal constructor. What's wrong and How to fix it?
class FancyButton extends HTMLButtonElement {
constructor() {
super(); // always call super() first in the ctor.
this.addEventListener('click', e => this.drawRipple(e.offsetX,e.offsetY));
}
// Material design ripple animation.
drawRipple(x, y) {
let div = document.createElement('div');
div.classList.add('ripple');
this.appendChild(div);
// div.style.top = `${y - div.clientHeight/2}px`;
// div.style.left = `${x - div.clientWidth/2}px`;
div.style.backgroundColor = 'currentColor';
div.classList.add('run');
div.addEventListener('transitionend', e => div.remove());
}
}
customElements.define('fancy-button', FancyButton, {extends: 'button'});
let button = new FancyButton();
button.textContent = 'Fancy button!';
button.disabled = true;
推荐答案
闪烁,当前实现Custom Element v1的Web引擎(例如, Chrome v53 + )仅支持 :请参阅open 闪烁的bug 。
Blink, the web engine that currently implements Custom Element v1 (in Chrome v53+ for example) only supports autonomous custom elements: see open Blink bug.
如果你想定义< href =https://html.spec.whatwg.org/multipage/scripting.html#custom-elements-customized-builtin-examplerel =nofollow noreferrer>定制的内置元素 (即< button>
扩展名),则需要使用像来自Web Reflection 的一个。
If you want to define customized built-in elements (i.e. <button>
extension), you'll need to use a polyfill like the one from Web Reflection.
另外,你c仍然使用自定义元素v0语法( document.registerElement
)。
Alternatly, you can still use the Custom Element v0 syntax (document.registerElement
).
这篇关于如何创建一个扩展类的自定义元素的新实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!