본문 바로가기
IT/스벨트(Svelte)

스벨트 부모 컴포넌트에서 자식 컴포넌트의 함수 호출 방법

by Blog37 2024. 5. 23.
반응형

자식 컴포넌트 코드

<script>
  let count = 0;
  export const increment = () => {
    count += 1;
  }
</script>

<p>{count}</p>

 

부모 컴포넌트 코드

<script>
  import Child from './Child.svelte';
  let childComponent;
  
  // let call;
</script>

<Child bind:this={childComponent} />
<button on:click={()=>{ childComponent.increment() }}>
    자식 컴포넌트의 함수 호출
</button>

<!--
<Child bind:increment={call} />
<button on:click={()=>{ call() }}>
    자식 컴포넌트의 함수 호출2
</button>
-->

 

반응형