Timo
Timo

Reputation: 87

Access child data in parent component

How can I get access to child data in a parent component?

This is the parent component:

<script>
  import Search from './components/Search.svelte'
</script>

<Search />

This is the child component:

<script>
  export let term
</script>

<input bind:value={term} />

Thanks!

Upvotes: 2

Views: 990

Answers (1)

Stephane Vanraes
Stephane Vanraes

Reputation: 16411

In the parent you can do

<script>
 import Search from './components/Search.svelte'
 let term
</script>

<Search bind:term/>

That way the term in the parent will be synchronized with the one in the Search component.

Another approach is to get a reference to the entire component

<script>
 import Search from './components/Search.svelte'
 let searchComponent
</script>

<Search bind:this={searchComponent}/>

and then you can access exported props and functions using searchComponent.***

Upvotes: 5

Related Questions