$$props and $$restProps
在符文模式下,使用$props符文获取包含所有传入属性的对象很容易。
在传统模式下,我们使用 $$props 和 $$restProps:
$$props包含了所有传入的 props,包括那些没有使用export关键字单独声明的$$restProps包含了所有传入的 props,除了那些单独声明的
例如,一个 <Button> 组件可能需要将其所有属性传递给其自身的 <button> 元素,除了 variant 属性:
<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会带来一定的性能损失,因此只有在需要时才应使用它们。
Edit this page on GitHub llms.txt
previous next