【发布时间】:2021-06-07 00:38:12
【问题描述】:
我想构建一个可以与 React 框架一起使用的 svelte 组件,在阅读了 svelte 文档之后,我决定将我的 Svelte 组件构建为 svelte customElement,一切都很顺利,直到我使用了@smui/dialog,我的代码:
<svelte:options tag="charity-legal-agreement-element" />
<script lang="ts">
import Dialog, {Title, Content, Actions} from '@smui/dialog';
...
<Dialog
bind:this={dialog}
aria-labelledby="dialog-title"
aria-describedby="dialog-content"
on:MDCDialog:closed={deleteItem}
>
...
</Dialog>
汇总配置:
svelte({
preprocess: sveltePreprocess({ sourceMap: !production }),
compilerOptions: {
// enable run-time checks when not in production
dev: !production,
customElement: true
},
// customElement: true
}),
postcss({
extract: true,
minimize: true,
use: [
['sass', {
includePaths: [
'./src/theme',
'./node_modules',
'../../node_modules',
]
}]
]
}),
...
来自调试控制台的错误:
index.mjs:1512 Uncaught TypeError: Illegal constructor
at new SvelteElement (index.mjs:1512)
at new Dialog (Dialog.svelte:56)
at create_fragment (index.svelte:53)
at init (index.mjs:1489)
at new Src (index.svelte:72)
at main.ts:13
at main.ts:18
SvelteElement @ index.mjs:1512
Dialog @ Dialog.svelte:56
create_fragment @ index.svelte:53
init @ index.mjs:1489
Src @ index.svelte:72
(anonymous) @ main.ts:13
(anonymous) @ main.ts:18
如果我删除 @smui/dialog ,一切正常,但我需要对话框作为基础组件,任何想法都值得赞赏~
【问题讨论】:
-
您还必须为每个 Web 组件编写一个 React 包装器组件,因为 React 现在只有 71% 支持 W3C Web 组件标准。见custom-elements-everywhere.com/libraries/react/results/…
标签: svelte custom-element svelte-component