网站搜索

在 Svelte 中使用 TypeScript 无法向组件传递 prop


我有这个带有 title 属性的 BigButton 组件

<script lang="ts">
let title: string;
</script>
<h1 class="text-2xl mb-4 font-extrabold">{title}</h1>

然后在我的 index.svelte 中,我尝试以不同的方式传递 prop,但都不起作用:

<BigButton title={'加入游戏'} /> <BigButton title='加入游戏' />

lint 给了我以下错误:

Type '{ title: string; }' is not assignable to type 'IntrinsicAttributes'.
  Property 'title' does not exist on type 'IntrinsicAttributes'.

答案#1

应该使用 export 导出它,比如:

export let title: string;

Svelte 使用 export 关键字将变量声明标记为属性或 prop,这意味着它可以被组件的使用者访问