盛世宏图 发表于 2024-11-27 02:15:44

vue3 发送 axios 哀求时没有担当到响应数据

<script setup>
import Edit from './components/Edit.vue'
import axios from 'axios'
import { onMounted,ref } from 'vue'

// TODO: 列表渲染
//装数据的列表
const list = ref([])
const count = ref(0)
const getList = async () => {
//通过发送 /list 请求从后端拿到列表数据
const res = axios.get('/list')
list.value = res.data
count.value++
}
onMounted(() => getList)
</script> 一开始一直猜疑是后端接口的问题,大概是前端哀求路径的问题
最后排查了半天,通过 count 自增发现 getList 函数根本没有调用
检查 onMounted() 函数发现 是因为 getList 没有加括号 ()
精确写法:
<script setup>
import Edit from './components/Edit.vue'
import axios from 'axios'
import { onMounted,ref } from 'vue'

// TODO: 列表渲染
//装数据的列表
const list = ref([])
const count = ref(0)
const getList = async () => {
//通过发送 /list 请求从后端拿到列表数据
const res = axios.get('/list')
list.value = res.data
count.value++
}
onMounted(() => getList())
</script> 修改后乐成吸收到哀求的参数:
https://i-blog.csdnimg.cn/direct/c5b072d81e0b4164bea724074886032d.png

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: vue3 发送 axios 哀求时没有担当到响应数据