{#await ...}
{#await expression}...{:then name}...{:catch name}...{/await}{#await expression}...{:then name}...{/await}{#await expression then name}...{/await}{#await expression catch name}...{/await}Await 块允许您根据 Promise 的三种可能状态(等待中、已完成或已拒绝)进行分支。
{#await promise}
<!-- promise is pending -->
<p>waiting for the promise to resolve...</p>
{:then value}
<!-- promise was fulfilled or not a Promise -->
<p>The value is {value}</p>
{:catch error}
<!-- promise was rejected -->
<p>Something went wrong: {error.message}</p>
{/await}在服务器端渲染期间,只会渲染等待中分支。
如果提供的表达式不是
Promise,则只会渲染:then分支,包括在服务器端渲染期间。
如果不需要在 promise 拒绝时渲染任何内容(或没有错误可能),则可以省略 catch 块。
{#await promise}
<!-- promise is pending -->
<p>waiting for the promise to resolve...</p>
{:then value}
<!-- promise was fulfilled -->
<p>The value is {value}</p>
{/await}如果您不关心等待状态,也可以省略初始块。
{#await promise then value}
<p>The value is {value}</p>
{/await}类似地,如果您只想显示错误状态,则可以省略 then 块。
{#await promise catch error}
<p>The error is {error}</p>
{/await}您可以将
#await与import(...)一起使用以延迟渲染组件{#await import('./Component.svelte') then { default: Component }} <Component /> {/await}
上一页 下一页