vue3在setup中获取异步数据并暴露出去
作者:互联网
使用生命周期钩子onBeforeMount
setup(){
const postData = ref([]) onBeforeMount(async () => { const res = await axios.get('http://localhost:3002/postdata') postData.value = res.data }) return { ... postData } }
const postData = ref([])要加上ref,不然获取不到,踩了一遍坑
标签:异步,const,postData,res,setup,onBeforeMount,vue3,ref 来源: https://www.cnblogs.com/lijun12138/p/16327856.html