$$props 和 $$restProps
在符文模式中,使用 $props
符文可以輕鬆取得包含所有傳入 props 的物件。
在舊版模式中,我們使用 $$props
和 $$restProps
$$props
包含所有傳入的 props,包括那些沒有使用export
關鍵字單獨宣告的 props$$restProps
包含所有傳入的 props,除了 那些被單獨宣告的 props
例如,一個 <Button>
元件可能需要將所有 props 傳遞給它自己的 <button>
元素,除了 variant
prop
<script>
export let variant;
</script>
<button {...$$restProps} class="variant-{variant} {$$props.class ?? ''}">
click me
</button>
<style>
.variant-danger {
background: red;
}
</style>
在 Svelte 3/4 中,使用 $$props
和 $$restProps
會產生適度的效能損失,因此只有在需要時才應使用它們。
上一頁 下一頁