可能是我在 Google 中输入了错误的内容,无法得到好的答案。
是否有一种“推荐的精简”方法来存储 GET 结果的值,以便在每次刷新或链接切换时,存储中的结果都在组件中使用,直到超时(再次调用 api)?
我的目的是从外部 API 获取博客文章并在列表中显示它们,但不是在每次刷新或链接切换时显示它们。
我的代码:
<script>
let posts = [];
onMount(async () => {
const res = await fetch(apiBaseUrl + "/blogposts");
posts = await res.json();
});
</script>
{#each posts as post}
<h5>{post.title}</h5>
{/each}
Run Code Online (Sandbox Code Playgroud)
在伪代码中我想要的是:
if (store.blogposts.timeout === true){
onMount(...);
// renew component
}
Run Code Online (Sandbox Code Playgroud)