赞
踩
坑有千千万,汇总下目前的解决办法,也因为对框架的不够深入,掌握得有限,大家有更好的,请指教【抱拳】
const { data } = await useFetch(
'http://www.baidu.com/xx-list',
{
method: 'get',
key: uuid(),
headers: {
Authorization: 'xxx'
},
params: {
page: currentPage.value,
size: 10
}
}
)
console.log('data', data)
刷新的时候打印结果data.value是null
解决1: 加个定时器(不友好)
<script lang="ts" setup> const { data } = await useFetch( 'http://www.baidu.com/xx-list', { method: 'get', key: uuid(), headers: { Authorization: 'xxxx' }, params: { page: currentPage.value, size: 10 } } ) setTimeout(() => { console.log('data', data) }, 500); </script>
解决2: 函数方法+await nextTick(),把获取数据接口放在方法里,方法里使用await nextTick(),然后调用方法
<script lang="ts" setup> import { nextTick } from 'vue' const feachData = async (key = '') => { await nextTick() const { data, pending, error, refresh } = await useFetch( 'http://www.baidu.com/xx-list', { method: 'get', key: uuid(), headers: { Authorization: 'xxx' }, params: { page: currentPage.value, size: 10 } } ) console.log('data', data.value) } feachData() <script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。