Svelte - 组件一如何使用组件二的功能?

Pro*_*ess 4 components svelte

CompOne 如何运行 CompTwo 中的“test”功能?

CompOne.svelte

<script>
   import {test} from './CompTwo.svelte'
</script>
<!-- Some style and HTML tags for this component -->
Run Code Online (Sandbox Code Playgroud)

CompTwo.svelte

<script>
   export const test = () => { console.log('testing function') }
</script>
<!-- Some style and HTML tags for this component -->
Run Code Online (Sandbox Code Playgroud)

Ste*_*aes 13

如果您有此组件的实例并绑定到它,则可以运行子函数。

应用程序.svelte

<script>
  import Component from './Component.svelte';   
  let comp;
</script>

<Component bind:this={comp} />
<button on:click={() => comp.test()}>Do Stuff</button>
Run Code Online (Sandbox Code Playgroud)

组件.svelte

<script>
    export const test = () => console.log('testing');
</script>
Run Code Online (Sandbox Code Playgroud)

工作示例