简体   繁体   English

扩展另一个自定义元素?

[英]Extending another custom element?

Trying to extend an custom element:尝试扩展自定义元素:

 class ABC extends HTMLDivElement { connectedCallback() { console.log('ABC here') } } customElements.define("x-abc", ABC, { extends: 'div' }) class XYZ extends ABC { connectedCallback() { console.log('XYZ here') } } customElements.define("x-xyz", XYZ, { extends: 'x-abc' })
 <div is="x-abc"></div> <div is="x-xyz"></div>

but getting this error:但收到此错误:

 Uncaught DOMException: Failed to execute 'define' on 'CustomElementRegistry': "x-abc" is a valid custom element name

The err is confusing because it say 'is valid', not 'is not valid' !!错误令人困惑,因为它说“有效”,而不是“无效”!

On the other hand FF says:另一方面,FF 说:

  Uncaught DOMException: CustomElementRegistry.define: 'x-xyz' cannot extend a custom element

I want to build hierarchy of custom elements.我想建立自定义元素的层次结构。

The error may sound confusing at first, but actually it is very clear.这个错误起初可能听起来令人困惑,但实际上它非常清楚。 The spec clearly defines that you can only extend built-ins using the is syntax.规范明确定义您只能使用is语法扩展内置函数。 As it is currently designed, using ABC as a base class is definitely not the brightest idea (until you drop extending HTMLDivElement in favour of HTMLElement ).按照目前的设计,使用ABC作为基础 class 绝对不是最聪明的想法(除非您放弃扩展HTMLDivElement以支持HTMLElement )。

You have not explained why you need to extend div elements, which is definitely not what you typically build a component hierarchy on.您还没有解释为什么需要扩展div元素,这绝对不是您通常构建组件层次结构的基础。 It is not even (and probably never will be) supported in Safari. Safari 甚至不(可能永远不会)支持它。

Instead, extend HTMLElement , and omit the is -syntax.相反,扩展HTMLElement并省略is -syntax。

 class ABC extends HTMLElement { connectedCallback() { console.log('ABC here') } } customElements.define('x-abc', ABC) class XYZ extends ABC { connectedCallback() { super.connectedCallback(); console.log('XYZ here') } } customElements.define('x-xyz', XYZ)
 <x-abc></x-abc> <x-xyz></x-xyz>

If for whatever strange reason you need to keep extending HTMLDivElement (instead of HTMLElement which would be the proper way), here you go:如果出于某种奇怪的原因您需要继续扩展HTMLDivElement (而不是HTMLElement ,这将是正确的方法),这里是 go:

Just replace the {extends: 'x-abc'} part with {extends: 'div'} .只需将{extends: 'x-abc'}部分替换为{extends: 'div'}

Both your components extend div (their defining tag name).您的两个组件都扩展了div (它们的定义标签名称)。

 class ABC extends HTMLDivElement { connectedCallback() { console.log('ABC here') } } customElements.define('x-abc', ABC, { extends: 'div' }) class XYZ extends ABC { connectedCallback() { super.connectedCallback(); console.log('XYZ here') } } customElements.define('x-xyz', XYZ, { extends: 'div' })
 <div is="x-abc"></div> <div is="x-xyz"></div>

Actually, the option-name extends:"xxx" is confusing.实际上,选项名称 extends:"xxx" 令人困惑。 As the extended built-in element must be html-marked with the tag of that built-in (accompanied with the is attribute), this option does not determine the direct ancestor ("extending" it) (this is defined by the class-constructor-chain), rather it defines the tag-name, for which the given constructor (together with the whole chain) is used when parsing html/upgrading the element.由于扩展的内置元素必须使用该内置元素的标记进行 html 标记(伴随 is 属性),因此此选项不确定直接祖先(“扩展”它)(这是由类定义的-构造函数链),而是定义标签名称,在解析 html/升级元素时使用给定的构造函数(连同整个链)。 Strictly speaking, it does extend that tag's class indeed, however not directly.严格来说,它确实扩展了该标签的 class,但不是直接扩展。 The meaning "extends", is more related to the html is="xxxx" syntax. “扩展”的意思,与 html is="xxxx" 语法更相关。 Think about it as it would have some name like basetag, builtintag, markuptag, builtinbase etc.想想它,因为它会有一些名称,如 basetag、builtintag、markuptag、builtinbase 等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM