vue3 onMounted異步函數(shù)同步請求async/await實現(xiàn)
項目需求
在項目中遇到一個問題,就是打開頁面時,顯示的不正確,在onMounted(掛載完數(shù)據(jù)) 中初始化來的數(shù)據(jù)沒渲染上, 這是因為,數(shù)據(jù)重新賦值沒在onUpdated(更新完data 數(shù)據(jù))之前, 而是在之后執(zhí)行的, 需要在onMounted鉤子函數(shù)中增加async/await;
特別注意: 在onMounted中,從API請求到的數(shù)據(jù), 賦值給響應(yīng)式data 數(shù)據(jù),建議只賦值一次
代碼
//導(dǎo)入使用的API import { reactive, toRefs, getCurrentInstance,onBeforeMount, onMounted,onBeforeUpdate,onUpdated,onBeforeUnmount, onUnmounted, watch,computed} from "vue" export default{ components: {xx}, props: {}, setup(props, context){ console.log("*******setup******") //獲取當(dāng)前實例 const {ctx, proxy} = getCurrentInstance() // 定義響應(yīng)式data 數(shù)據(jù) const state = reactive({ }) console.log("*******end reactive******") // 定義方法 const methods = { async dealData(firstResData){ if(firstResData.status == "finished"){ state.collection_data = firstResData.data state.colection_id = firstResData.id await get(api+state.colection_id).then((resData) => { state.layoutX = resData.x state.layoutY = resData.y console.log("state.layoutX"+state.layoutX) console.log("state.layoutY"+state.layoutY) }) } }, // ***********async/await 實現(xiàn)請求同步功能************** async refreshData(){ await get(api).then(firstResData=>{ console.log("state.curStatus="+firstResData.status) methods.dealData(firstResData) }).catch(()=>{ }) }, } console.log("*******end methods******") onBeforeMount(()=>{ // dom 掛載前 console.log("*******onBeforeMount******") }) onMounted(async()=>{ //dom 掛載后 console.log("*******onMounted******") state.collection_id = proxy.$route.query.id await methods.init() }) onBeforeUpdate(()=>{ //對響應(yīng)式data數(shù)據(jù)有更新, 更新前 console.log("*******onBeforeUpdate******") }) onUpdated(()=>{ //對響應(yīng)式data數(shù)據(jù)有更新, 更新后 console.log("*******onUpdated******") }) onBeforeUnmount(()=>{ //銷毀頁面組件前, 即關(guān)閉 console.log("*******onBeforeUnmount******") }) onUnmounted(()=>{ //銷毀后 console.log("*******onUnmounted******") }) return { ...toRefs(state), ...methods, } }
打印結(jié)果
*******setup******
*******end reactive******
*******end methods******
*******onBeforeMount******
*******onMounted******
*******onBeforeUpdate******
*******onUpdated******
state.curStatus=3
state.layoutX150
state.layoutY280
*******onBeforeUpdate******
##############################以下是子組件中打印的信息,可以不關(guān)注
*******setup in MyXgPlayer******
props.layoutX=150
props.layoutY=280
*******onBeforeMount in MyXgPlayer******
*******setup in MyXgPlayer******
props.layoutX=150
props.layoutY=280
*******onBeforeMount in MyXgPlayer******
*******onUpdated******
*******onMounted in MyXgPlayer*******
*******onMounted in MyXgPlayer*******
以上就是vue3 onMounted異步函數(shù)同步請求async/await實現(xiàn)的詳細內(nèi)容,更多關(guān)于vue3 onMounted async/await的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子
今天小編就為大家分享一篇Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-10-10vue3中處理不同數(shù)據(jù)結(jié)構(gòu)JSON的實現(xiàn)
本文主要介紹了vue3中處理不同數(shù)據(jù)結(jié)構(gòu)JSON的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06vue?router進行路由跳轉(zhuǎn)并攜帶參數(shù)的實例詳解(params/query)
在使用`router.push`進行路由跳轉(zhuǎn)到另一個組件時,可以通過`params`或`query`來傳遞參數(shù),這篇文章主要介紹了vue?router進行路由跳轉(zhuǎn)并攜帶參數(shù)(params/query),需要的朋友可以參考下2023-09-09