简体   繁体   English

如何在 Svelte 中绑定来自子组件的输入值?

[英]How to bind input value from child component in Svelte?

ideal理想的

I'd like to get input data from child component.我想从子组件获取输入数据。

What I have tried我试过的

<script>
  import Input from "./Input.svelte";
  let userGoal = "";
</script>

<h1>Your Goal is {userGoal}</h1>

<Input {userGoal} />
<script>
  export let userGoal = "";

  $: console.log(userGoal);
</script>

<input type="text" bind:value={userGoal} />

$: console.log(userGoal); shows userGoal at each event which is as I expected.正如我所料,在每个事件中显示 userGoal。 However, It doesn't affect to parent Component.但是,它不会影响父组件。

Summary概括

I'm new to Svelte.我是 Svelte 的新手。 Any help is appreciated.任何帮助表示赞赏。

Just change <Input {userGoal}/> to this:只需将<Input {userGoal}/>更改为:

<Input bind:userGoal/>

Demo here . 演示在这里 If you want to call it something else in the parent, do bind:userGoal={somethingElse} .如果您想在父级中将其称为其他名称,请执行bind:userGoal={somethingElse}

Here's a tutorial on component bindings.这是有关组件绑定的教程

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM