You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在SvelteKit项目引入SMUI组件时出现单script实例错误如何解决?

报错成因

Svelte 组件语法有明确规定:单个组件内只能存在1个不带context="module"属性的实例级<script>标签,带context="module"的模块级脚本不受该规则限制。你当前的代码里写了2个普通实例级<script>,属于原生Svelte语法错误,和引入SMUI组件库没有直接关联。

解决方法

将两个实例级<script>的内容合并为一个即可,不需要调整任何额外配置项,修改后的完整代码如下:

<script context="module" lang="ts">
    export const prerender = true;
</script>

<script lang="ts">
    import Counter from '$lib/Counter.svelte';
    import Button, { Label } from '@smui/button';
   
    let clicked = 0;
</script>

<svelte:head>
    <title>Home</title>
</svelte:head>

<section>
    <h1>
        <div class="welcome">
            <picture>
                <source srcset="svelte-welcome.webp" type="image/webp" />
                <img src="svelte-welcome.png" alt="Welcome" />
            </picture>
        </div>

        to your new<br />Svelte 
        <Button variant="raised">
            aaaaaaa
        </Button>
    </h1>

    <h2>
        try editing <strong>src/routes/index.svelte</strong>
    </h2>

    <Counter />
</section>

合并后SMUI组件可以正常调用,不会再触发该报错。

内容的提问来源于stack exchange,提问作者ninsau

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 09:45:05