简体   繁体   English

如何在 Svelte 组件中接收任意道具并传递给子组件?

[英]How can I receive arbitrary props in a Svelte component and pass to a child component?

I want to receive arbitrary props from "above" and spread them onto an <input> , as shown here where inputProps would become an object containing any additional props set on this component (similar to python's **kwargs , in case you're familiar):我想从“above”接收任意道具并将它们传播到<input> ,如下所示,其中inputProps将成为包含在此组件上设置的任何其他道具的对象(类似于 python 的**kwargs ,以防你熟悉):

<script>
export let id;
export ...inputProps;
</script>

<div>
    id: {id}
    <input {...inputProps} />
</div>

Can you point me toward the correct Svelte mechanism for accomplishing something like this?你能指出正确的 Svelte 机制来完成这样的事情吗? I have a feeling that I'm asking the wrong question, but I need a svelte developer to set me straight.我有一种感觉,我问错了问题,但我需要一个苗条的开发人员来纠正我。 Should I use a slot instead?我应该改用插槽吗? Or learn about actions / "the use directive"?或者了解操作/“使用指令”?

You can use $$props to access all the props given to a component.你可以使用$$props来访问所有赋予组件的 props。

$$props references all props that are passed to a component – including ones that are not declared with export . $$props引用所有传递给组件的 props——包括那些没有用export声明的 props。 It is useful in rare cases, but not generally recommended, as it is difficult for Svelte to optimise.它在极少数情况下很有用,但通常不推荐使用,因为 Svelte 很难优化。

Example ( REPL )示例 ( REPL )

<!-- App.svelte -->
<script>
  import Child from './Child.svelte';
</script>

<Child id="foo" placeholder="bar" />

<!-- Child.svelte -->
<script>
  let id, inputProps;
  $: ({ id, ...inputProps } = $$props);
</script>

<div>
  id: {id}
  <input {...inputProps} />
</div>

Svelte now also offers $$restProps . Svelte 现在还提供$$restProps See the Svelte API Docs - Attributes and props .请参阅Svelte API 文档 - 属性和道具

$$props references all props that are passed to a component – including ones that are not declared with export. $$props引用所有传递给组件的 props——包括那些没有用 export 声明的 props。 It is useful in rare cases, but not generally recommended, as it is difficult for Svelte to optimise.它在极少数情况下很有用,但通常不推荐使用,因为 Svelte 很难优化。

<Widget {...$$props}/>

$$restProps contains only the props which are not declared with export. $$restProps仅包含未通过导出声明的道具。 It can be used to pass down other unknown attributes to an element in a component.它可用于将其他未知属性传递给组件中的元素。

<input {...$$restProps}>

While exporting you don't need to use the spread operator导出时您不需要使用扩展运算符

<script>
 export let id;
 export inputProps;
</script>

<div>
 id: {id}
 <input {...inputProps} />
</div>

Working example with some sample code带有一些示例代码的工作示例

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Svelte:如何将数据或道具从子组件传递给父组件? - Svelte: How to pass data or props from a child component to the parent? (苗条)如何从组件访问以前的组件道具 - (svelte) how can I access previus component props from component 当我通过 props 从父组件将变量的值传递给子组件时,我收到一个未定义的值 - I receive an undefined value when I pass the value of a variable through props from a parent component to a child component 如何通过道具[REACT.JS]将图片网址从父组件传递到子组件 - How can I pass image url from parent component to child component via props [REACT.JS] 如何将道具传递给样式组件中的基础组件? - How can I pass props to base component in styled-component? 如何将所有事件作为道具传递给svelte中的组件 - How to pass all event down as props to a component in svelte 如何在单个组件中渲染多个道具,然后将这些道具传递给 React 中的子组件 - How do i render multiple props in a single component and then pass those props to a child component in React 如何访问对象变量的字段作为道具传递给它的子组件? - How can I access the object variable's fields to pass as the props to its child component? 如何使用重新选择将道具传递到包装在容器中的子组件? - How can I pass props down to a child component that is wrapped in a container using reselect? 如何将相同的道具传递给Component中的每个孩子 - How pass the same props to every child in Component
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM