【问题标题】:Vanilla JavaScript Custom Elements TypeErrorVanilla JavaScript 自定义元素类型错误
【发布时间】:2018-09-27 00:42:36
【问题描述】:

我的问题是关于为自定义元素扩展 HTMLElement。

class Foo extends HTMLElement { ... }

class Bar extends Foo { ... }

class FooBar extends Bar { ... }

他们每个人都在构造函数中调用 super()。

当我使用 Bar 定义自定义元素时,我没有收到任何错误

customElements.define( 'custom-tag', Bar );
or
customElements.define( 'custom-tag', class Bar extends Foo { ... } );

我正在尝试使用 FooBar 类定义自定义元素。

customElements.define( 'other-custom-tag', FooBar );
or
customElements.define( 'other-custom-tag', class FooBar extends Bar { ... } );

但我得到了

Uncaught TypeError: Failed to execute 'define' on 'CustomElementRegistry':
The provided value cannot be converted to a sequence.

通过 Mozilla 文档

https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/define

TypeError   The referenced constructor is not a constructor.

我找不到与此问题相关的任何内容...

【问题讨论】:

  • customElements.define 会抛出哪个错误? “不能转换为序列”还是“不是构造函数?”
  • "无法转成序列"
  • 我高度怀疑这是由于您的自定义元素的实现细节造成的错误。您可以发布一个可以重现错误的课程吗?
  • 我正在尝试重现该错误。我认为它来自静态 getobservedAttributes 方法。
  • 你能提供更多与你的类相关的代码吗?你的构造函数是什么样的?

标签: javascript class custom-element


【解决方案1】:

问题很可能是static get observedAttributes() - 在我的情况下,我有一个愚蠢的错字,在一个数组上使用push,它返回新数组元素的count,而不是数组本身(替换为concat,就像一个魅力)。

换句话说,只要确保静态 getter 返回一个数组就可以了 ?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-09-19
    • 1970-01-01
    • 2020-10-02
    • 2020-06-21
    • 2018-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多