【发布时间】:2021-12-08 17:12:09
【问题描述】:
我正在尝试使用 Svelte 的第一个项目,但我遇到了这样的情况:
我从如下 API 获取结果列表:
# Page with a list of items
<script context="module">
export const load = async ({ page, fetch }) => {
const res = await fetch(`url`);
const items = await res.json();
return {
props: {
items,
},
};
};
</script>
<script>
export let items;
</script>
{#each items.data as { id, item, href }}
<li>
<a href="/page/{href}">{ item }</a>
</li>
{/each}
我正在尝试将多个参数传递给下一个(动态)路由,以便 href 是 slug 和 id 是 id 用于从 API 获取项目详细信息,如下所示:
# Page with a single item
<script context="module">
export const load = async ({ page, fetch }) => {
const slug = page.params.slug; <-- I can get this
const res = await fetch(`url/${page.id}`); <-- but not this
const data = await res.json();
return {
props: {
data,
},
};
};
</script>
我为我的无知道歉,但我真的很挣扎,我不知道如何准确地解决这个问题。我会很感激任何提示。
【问题讨论】:
标签: javascript svelte dynamic-routing