简体   繁体   English

无法使Web组件在codeandbox.io中工作

[英]Cannot get web component to work in codesandbox.io

I'm trying to get a customized built-in web component to work in codesandbox.io. 我正在尝试获得一个自定义的内置Web组件,以在codeandbox.io中工作。 This is the code: 这是代码:

 class MyDiv extends HTMLDivElement { constructor() { super(); } connectedCallback() { this.innerHTML = "works!"; } } customElements.define("my-div", MyDiv, {extends: 'div'}); 
 <div is="my-div"></div> 

The error I'm getting: 我得到的错误:

Failed to construct 'HTMLDivElement': Please use the 'new' operator, this DOM object constructor cannot be called as a function.

Tested in Chrome 67, Arch Linux. 已在Arch Linux的Chrome 67中进行了测试。 Here's the link: https://codesandbox.io/s/yqln560jzj 这是链接: https : //codesandbox.io/s/yqln560jzj

It does work here in a snippet, and it also works on codepen: https://codepen.io/connexo/pen/ZjEbqo 它确实可以在代码段中运行,也可以在Codepen上运行: https ://codepen.io/connexo/pen/ZjEbqo

What am I missing here? 我在这里想念什么?

If it doesn't works it's because Codesandbox is using a kind a Javascript preprocessor (Typescript?) on external JS files, that will execute the code before (to detect syntaxt errors or so on). 如果它不起作用,是因为Codesandbox在外部JS文件上使用了一种Javascript预处理程序(Typescript?),它将在执行该代码之前(以检测语法错误等)。

The customElements.define() method is called twice, and fails the second time it is called because you can define a custom element only one time. customElements.define()方法被调用两次,第二次调用失败,因为您只能定义一个自定义元素。

You can see it by calling customElements.get() inside your script, and see it's already defined. 您可以通过在脚本内调用customElements.get()来查看它,并查看它是否已定义。

console.log( customElements.get( 'my-div' ) )

If you put the Javascript inline (in the HTML file index.html ) in a element it will work fine. 如果将Javascript内联(在HTML文件index.html中 )放在一个元素中,它将正常工作。

Note that the second error you get it's because you are using the Babel preprocessor in the Codeopen snippet. 请注意,您得到的第二个错误是因为您在Codeopen代码段中使用了Babel预处理程序。 You must disable Babel and it will work. 您必须禁用Babel才能使用。

What am I missing here? 我在这里想念什么?

Nothing. 没有。 It's codesandbox.io that is missing Custom Elements support. 缺少自定义元素支持的是codesandbox.io。

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

相关问题 如何在“codesandbox.io”上配置 ESLint / Prettier 格式规则 - How to configure ESLint / Prettier formatting rules on "codesandbox.io" codesandbox.io无法指向任何以“ index.html”开头的内容 - codesandbox.io unable to point to anything expcet “index.html” Codesandbox.io 引发了跨域错误问题 - A cross-origin error was thrown problem with Codesandbox.io 无法通过 ERR_CERT_COMMON_NAME_INVALID 在 CodeSandbox.io 上获取 API? - Failed to fetch API on CodeSandbox.io with ERR_CERT_COMMON_NAME_INVALID? Codesandbox,io 是否从 REACT 应用程序中删除公共目录?为什么没有它它可以工作? - Does Codesandbox,io remove the public directory from REACT apps & Why does it work without it? Vue套接字io不起作用(无法触发连接事件) - Vue socket io does not work (cannot get it to fire connection events) 组件渲染两次? 是codeandbox的问题吗? - Is component rendered twice? Is it codesandbox problem? 无法使socket.io-emitter与socket.io-redis一起使用 - cannot get socket.io-emitter to work with socket.io-redis 无法使D3.js在Svelte组件内运行(使用汇总) - Cannot get D3.js to work inside Svelte component (with Rollup) scrollToTop 在反应中不起作用(代码沙盒) - scrollToTop doesn't work in react (codesandbox)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM