【发布时间】:2022-01-03 23:55:57
【问题描述】:
在 Sveltekit 中,我需要找到一种通过向不同页面发出请求来触发状态更改的方法。
说:
Index.svelte
<script>
import { count } from '../stores.js';
</script>
{$count}
stores.js
import { writable } from 'svelte/store';
export const count = writable(0);
现在,当我访问 localhost:3000/api 时,我希望计数为 ++ 并反映在 index.svelte 文件中。
我试过了
api.svelte
<script>
import { count } from '../stores.js';
count.update(n => n + 1);
</script>
但这根本不起作用。例如,当我将代码放在 index.svelte 中的函数中并使用按钮触发它时,代码确实有效。有什么路线吗?
【问题讨论】: