Vue 设置脚本提前返回?
P粉497463473
P粉497463473 2023-08-29 19:25:34
[Vue.js讨论组]
<p>是否可以在 Vue 设置脚本中提前返回?</p> <pre class="brush:html;toolbar:false;">&lt;template&gt; &lt;div v-if=&quot;error || !data&quot;&gt;Fallback content...&lt;/div&gt; &lt;div v-else&gt;Page content...&lt;/div&gt; &lt;/template&gt; &lt;script setup lang=&quot;ts&quot;&gt; // ... const { data, error } = await fetchApi() // Early return here? E.g., if (error || !data) return // ... // Lots of code which doesn't need to run if error. // E.g., calculation of variables only used in the page content. // ... &lt;/script&gt; </pre> <p>注意:我来自 React 世界,对 Vue 还算陌生。</p>
P粉497463473
P粉497463473

全部回复(1)
P粉458913655

你可以抛出一个错误:

const {data, error} = await fetchApi()
if(error || !data) throw new Error('your error message')

它阻止其余代码运行

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号