nuxt3 useFetch 刷新或首次進入報錯
從其他頁面跳轉(zhuǎn)過來正常,但是刷新會報錯
<script lang="ts" setup> const positionOption = ref([]); const initData = () => { useFetch('/api/getTagsByKey', { query: { tagKey: 'position' }, method: 'get', }).then(res => { positionOption.value = res.data.value.data; }) } initData(); </script>

解決方法:添加 await nextTick(),但是這個方法會導(dǎo)致客戶端請求
<script lang="ts" setup> const positionOption = ref([]); const initData = async () => { await nextTick(); useFetch('/api/getTagsByKey', { query: { tagKey: 'position' }, method: 'get', }).then(res => { positionOption.value = res.data.value.data; }) } initData(); </script>
服務(wù)端請求方法:useFetch在最外層,不要放到方法中
<script lang="ts" setup> const positionOption = ref([]); useFetch('/api/getTagsByKey', { query: { tagKey: 'position' }, method: 'get', }).then(res => { positionOption.value = res.data.value.data; }) </script>

浙公網(wǎng)安備 33010602011771號