【发布时间】:2022-08-07 19:45:14
【问题描述】:
我在这里尝试做的事情可能是不可能的,但作为 Svelte 的新手,我希望它是。 ???
我在一个组件中有一个删除按钮,它打开一个全局可用的模式,用作确认对话框。模态组件位于我的__layout.svelte 中,因此我可以从应用程序的任何位置调用它。
//=== Modal.svelte ===
<script lang=\"ts\">
import { modal, confirmTrash } from \'$lib/stores/modal\'
//Do a bunch of stuff to customize the modal...
</script>
{#if modal.show}
<h2>{$modal.title}</h2>
<p>{$modal.message}</p>
<button on:click={() => { send confirmation that the delete was confirmed }>{$modal.button}</button>
{/if}
这是我的modal 商店:
//=== modal.ts ===
import { writable } from \'svelte/store\'
//Customize the modal\'s state
export const modal = writable({
title: \'\',
message: \'\',
button: \'\',
mode: \'\',
show: false
})
//Convenience function for showing the trash confirmation modal
export function confirmTrash(modalTitle: string, modalMessage: string, buttonText: string){
modal.set({
title: modalTitle,
message: modalMessage,
button: buttonText,
mode: \'trash\',
show: true
})
}
最后,这是我的应用程序中的组件,我通过单击显示删除确认模式的链接实际启动删除过程:
//=== Component.svelte ===
<script lang=\"ts\">
import { confirmTrash } from \'$lib/stores/modal\'
</script>
<a href=\"#trash\"
on:click={() => {
confirmTrash(\'Trash Title\', \'Message goes here.\', \'Delete\', function(result){
//I want to be able to know ** here ** if the user clicked \"Delete\"
console.log(result) //???
})
}}
>Trash</a>
我不清楚如何通过我的confirmTrash 函数连接回调函数,以将模态中的用户响应传递回调用模态的位置。这可能吗?
标签: javascript typescript callback svelte svelte-store