最后,还有核选项 — invalidateAll()
。这将不加区分地重新运行当前页面的所有 load
函数,而不管它们依赖什么。
¥Finally, there’s the nuclear option — invalidateAll()
. This will indiscriminately re-run all load
functions for the current page, regardless of what they depend on.
更新上一个练习中的 src/routes/[...timezone]/+page.svelte
:
¥Update src/routes/[...timezone]/+page.svelte
from the previous exercise:
<script>
import { onMount } from 'svelte';
import { invalidateAll } from '$app/navigation';
let { data } = $props();
onMount(() => {
const interval = setInterval(() => {
invalidateAll();
}, 1000);
return () => {
clearInterval(interval);
};
});
</script>
<script lang="ts">
import { onMount } from 'svelte';
import { invalidateAll } from '$app/navigation';
let { data } = $props();
onMount(() => {
const interval = setInterval(() => {
invalidateAll();
}, 1000);
return () => {
clearInterval(interval);
};
});
</script>
src/routes/+layout.js
中的 depends
调用不再是必要的:
¥The depends
call in src/routes/+layout.js
is no longer necessary:
export async function load({ depends }) {
depends('data:now');
return {
now: Date.now()
};
}
invalidate(() => true)
和invalidateAll
不一样。invalidateAll
还会重新运行load
函数,而没有任何url
依赖,而invalidate(() => true)
则不会。¥[!NOTE]
invalidate(() => true)
andinvalidateAll
are not the same.invalidateAll
also re-runsload
functions without anyurl
dependencies, whichinvalidate(() => true)
does not.