首页 > 其他分享 >nuxt3 useFetch 刷新或首次进入报错

nuxt3 useFetch 刷新或首次进入报错

时间:2024-04-03 11:47:40浏览次数:33  
标签:positionOption nuxt3 res initData 报错 useFetch const data

从其他页面跳转过来正常,但是刷新会报错

<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()

<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>   

 

标签:positionOption,nuxt3,res,initData,报错,useFetch,const,data
From: https://www.cnblogs.com/gxp69/p/18112310

相关文章