如何在 Svelte 中使用 Web 组件?

San*_*jay 6 javascript web-component svelte

我想使用https://github.com/microsoft/vscode-webview-ui-toolkit中的一些 Web 组件。但我不知道如何在 Svelte 中使用它们,因为 svelte 将 Web 组件视为 svelte 组件。

当我尝试将它们用作时,

<script lang="ts">
import { Button } from "@vscode/webview-ui-toolkit"
</script>

<main>
  <Button appearance="primary">Text</Button>
</main>

Run Code Online (Sandbox Code Playgroud)

我收到这个错误,

Element does not support attributes because type definitions are missing for this Svelte Component or element cannot be used as such.

Underlying error:
JSX element class does not support attributes because it does not have a '$$prop_def' property.ts(2607)
'Button' cannot be used as a JSX component.
  Its instance type 'Button' is not a valid JSX element.
    Property '$$prop_def' is missing in type 'Button' but required in type 'ElementClass'.

Possible causes:
- You use the instance type of a component where you should use the constructor type
- Type definitions are missing for this Svelte Component. If you are using Svelte 3.31+, use SvelteComponentTyped to add a definition:
  import type { SvelteComponentTyped } from "svelte";
  class ComponentName extends SvelteComponentTyped<{propertyName: string;}> {}ts(2786)
Run Code Online (Sandbox Code Playgroud)

Geo*_*ich 5

Button是一个自定义元素类,不应该像 Svelte 组件一样使用。您需要改用自定义元素标签,在本例中为<vscode-button>

<main>
  <vscode-button appearance="primary">Text</vscode-button>
</main>
Run Code Online (Sandbox Code Playgroud)

您可能需要进行额外的设置来初始化 Web 组件 - 我不熟悉这个库。