gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-05 01:14:23 28 4
gpt4 key购买 nike

CompOne 如何在 CompTwo 中运行函数“测试”?

CompOne.svelte

<script>
import {test} from './CompTwo.svelte'
</script>
<!-- Some style and HTML tags for this component -->

CompTwo.svelte

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

最佳答案

如果你有这个组件的实例并绑定(bind)到它,你可以运行子函数。

App.svelte

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

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

Component.svelte

<script>
export const test = () => console.log('testing');
</script>

Working example

关于components - Svelte - 组件一如何使用组件二的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58699393/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com