admin管理员组文章数量:1187328
I am writing a Svelte CSR application. I am wondering whether this is ok:
<script>
const data = api.getDataAsync()
<script>
{#await data}
<span>Loading</span>
{:then data}
//display data
{/await}
If not why? should I do it the react way
<script>
let data = $state(undefined)
onMount(()=>{
api.getDataSync().then(apiData=>data=apiData)
})
</script>
{#if !data}
<span>loading...</span>
{:else}
//display data
{/if}
Edit: Because I am using PocketBase, I have SSR turned off.
I am writing a Svelte CSR application. I am wondering whether this is ok:
<script>
const data = api.getDataAsync()
<script>
{#await data}
<span>Loading</span>
{:then data}
//display data
{/await}
If not why? should I do it the react way
<script>
let data = $state(undefined)
onMount(()=>{
api.getDataSync().then(apiData=>data=apiData)
})
</script>
{#if !data}
<span>loading...</span>
{:else}
//display data
{/if}
Edit: Because I am using PocketBase, I have SSR turned off.
Share Improve this question edited Jan 26 at 12:28 Oussam Larkem asked Jan 26 at 10:27 Oussam LarkemOussam Larkem 52 bronze badges1 Answer
Reset to default 0Neither, in SvelteKit use a load
function.
This is the standard mechanism for loading data which hooks into other mechanisms, including navigation where data can be pre-loaded on link hover or tap and a page is only shown when the data is loaded.
(If the load is known to take longer you can return a promise and #await
that on the page, but for data that load quickly, only showing the page once it is done gives a better experience without any layout shifts.)
本文标签: sveltekitShould I access the api directly inside the script tag in a svelte SPAStack Overflow
版权声明:本文标题:sveltekit - Should I access the api directly inside the script tag in a svelte SPA? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738320558a2074472.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论