vue3 onMounted異步函數(shù)同步請(qǐng)求async/await實(shí)現(xiàn)
項(xiàng)目需求
在項(xiàng)目中遇到一個(gè)問(wèn)題,就是打開(kāi)頁(yè)面時(shí),顯示的不正確,在onMounted(掛載完數(shù)據(jù)) 中初始化來(lái)的數(shù)據(jù)沒(méi)渲染上, 這是因?yàn)?,?shù)據(jù)重新賦值沒(méi)在onUpdated(更新完data 數(shù)據(jù))之前, 而是在之后執(zhí)行的, 需要在onMounted鉤子函數(shù)中增加async/await;
特別注意: 在onMounted中,從API請(qǐng)求到的數(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)前實(shí)例
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 實(shí)現(xiàn)請(qǐng)求同步功能**************
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(()=>{
//對(duì)響應(yīng)式data數(shù)據(jù)有更新, 更新前
console.log("*******onBeforeUpdate******")
})
onUpdated(()=>{
//對(duì)響應(yīng)式data數(shù)據(jù)有更新, 更新后
console.log("*******onUpdated******")
})
onBeforeUnmount(()=>{
//銷毀頁(yè)面組件前, 即關(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ù)同步請(qǐng)求async/await實(shí)現(xiàn)的詳細(xì)內(nèi)容,更多關(guān)于vue3 onMounted async/await的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
淺析vue中常見(jiàn)循環(huán)遍歷指令的使用 v-for
這篇文章主要介紹了vue中常見(jiàn)循環(huán)遍歷指令的使用 v-for,包括v-for遍歷數(shù)組,v-for遍歷json對(duì)象,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2018-04-04
vue大型項(xiàng)目之分模塊運(yùn)行/打包的實(shí)現(xiàn)
這篇文章主要介紹了vue大型項(xiàng)目之分模塊運(yùn)行/打包的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Vue-pdf實(shí)現(xiàn)在線預(yù)覽PDF文件
這篇文章主要為大家詳細(xì)介紹了Vue-pdf實(shí)現(xiàn)在線預(yù)覽PDF文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-05-05
Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子
今天小編就為大家分享一篇Vue axios 將傳遞的json數(shù)據(jù)轉(zhuǎn)為form data的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
Jenkins自動(dòng)化部署Vue項(xiàng)目的方法實(shí)現(xiàn)
本文主要介紹了Jenkins自動(dòng)化部署Vue項(xiàng)目的方法實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
vue3中處理不同數(shù)據(jù)結(jié)構(gòu)JSON的實(shí)現(xiàn)
本文主要介紹了vue3中處理不同數(shù)據(jù)結(jié)構(gòu)JSON的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
vue?router進(jìn)行路由跳轉(zhuǎn)并攜帶參數(shù)的實(shí)例詳解(params/query)
在使用`router.push`進(jìn)行路由跳轉(zhuǎn)到另一個(gè)組件時(shí),可以通過(guò)`params`或`query`來(lái)傳遞參數(shù),這篇文章主要介紹了vue?router進(jìn)行路由跳轉(zhuǎn)并攜帶參數(shù)(params/query),需要的朋友可以參考下2023-09-09

