如果下面的文件 test.svelte 是 /routes 中的一个页面,它会成功调用 load() 并使用它在我通过 http://localhost:3000/test 访问时检索到的 JSON 数组填充模板。如果我将此文件移动到 /lib 并将其作为 /routes/index.svelte 中的组件导入,则当我转到 http://localhost:3000 时,该组件的 load() 方法永远不会运行。
测试.svelte
<script context="module" lang="ts">
/**
* @type {import('@sveltejs/kit').Load}
*/
export async function load({ fetch }) {
const url = '/api/announcement'
const res: Response = await fetch(url)
if (res.ok) {
const sections: Announcement[] = await res.json()
return {
props: {
sections
}
}
}
return {
status: res.status,
error: new Error(`Could not load ${url}`)
}
}
</script>
<script lang="ts">
export let sections: …Run Code Online (Sandbox Code Playgroud)