vue在mounted拿不到props中傳遞的數(shù)據(jù)問題
vue mounted拿不到props中傳遞的數(shù)據(jù)
- 父組件向子組件傳遞參數(shù)
- 子組件使用props獲取
但是當(dāng)子組件中代碼異步的時候,可能會出現(xiàn)created和mounted中取不到傳遞過來的參數(shù)的情況。
export default { props: { courseDetail: { type: Object, }, }, data () { return { } }, watch:{ // 監(jiān)聽對象中的某個值 也可以直接監(jiān)聽某個對象 'courseDetail.id'(newValue,oldValue){ // 異步方法 this.getCommentList() } } }
vue props傳值問題 created mounted watch
props 傳值在子組件定義props 關(guān)于mounted created獲取問題
export default{ props:["name"], data(){ ? return{ ? ?data:this.name ? ?} }, created(){ console.log(data); // ?小明 console.log(this.name) // ?小明 }, mounted(){ console.log(data); // ?小明 console.log(this.name) // ?小明 }
當(dāng)props是動態(tài)獲取的時候AJAX請求獲取的數(shù)據(jù)
你的 name里面的值并不是固定的,而是動態(tài)獲取的,這種情況下,你會發(fā)現(xiàn) methods 中是取不到你的 chartData 的,或者取到的一直是默認(rèn)值。
比如下面這個情況
父組件
<template> <div id="app"> ?? ?<Child :name="data"></Child> </div> </template> export default{ props:["name"], data(){ ? return{ ? ?data:[] ? ?} }, mounted(){ this.getAddName(); }, methods:{ getAddName() { ?? ?axios.post(api,{params}).then(res=>{ ?? ? ?this.data = res.data.name ?? ?}).catch(err=>{ ?? ??? ?consloe.log(err); ?? ? ?}) ?? ?} ? } }
子組件Child
export default{ props:["name"], data(){ ? return{ ? ?data:[] ? ?} }, created(){ console.log(this.name) // unidenfied ?ajax異步獲取的值? }, mounted(){ console.log(this.name) // unidenfied ajax異步獲取的值? ?} }
解決動態(tài)獲取props取值傳值問題
這情況我是使用watch處理:
export default { ? ? props: ['name'], ? ? ? ? data(){ ? ? ? ? ? ? return { ? ? ? ? ? ? ? ? data: [] ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? watch: { ? ? ? ? ? ? //正確給 cData 賦值的 方法 ? ? ? ? ? ? name: function(newVal,oldVal){ ? ? ? ? ? ? ? ? this.data = newVal; ?//newVal即是name ? ? ? ? ? ? ? ? newVal && this.dataChild(); //newVal存在的話執(zhí)行dataChild函數(shù) ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? methods: { ? ? ? ? ? ? dataChild(){ ? ? ? ? ? ? ? ? //執(zhí)行其他邏輯 ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? mounted() { ? ? ? ? ? ? //在created、mounted這樣的生命周期, 給 this.data賦值會失敗,錯誤賦值方法 ? ? ? ? ? ? // this.data= this.name;? ? ? ? ? } }
監(jiān)聽 name 的值,當(dāng)它由空轉(zhuǎn)變時就會觸發(fā),這時候就能取到了,拿到值后要做的處理方法也需要在 watch 里面執(zhí)行。
數(shù)據(jù)渲染問題
props 渲染時,直接使用 在DOM上使用{{name}}渲染
針對于動態(tài)渲染DOM的操作問題:使用this.n e x t T i c k ( ) / / 等 待 渲 染 t h i s . nextTick() //等待渲染 this.nextTick()//等待渲染this.nextTick()將回調(diào)延遲到下次 DOM 更新循環(huán)之后執(zhí)行
export default { ? ? props: ['name'], ? ? ? ? data(){ ? ? ? ? ? ? return { ? ? ? ? ? ? ? ? data: [] ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? watch: { ? ? ? ? ? ? //正確給 cData 賦值的 方法 ? ? ? ? ? ? name: function(newVal,oldVal){ ? ? ? ? ? ? ? if(newVal){ //當(dāng)newVal 數(shù)據(jù)發(fā)生改變時 ? ? ? ? ? ? ? ? this.$nexTick(()=>{ //等待newVal 數(shù)據(jù)渲染完成 ?? ??? ??? ??? ?document.getElementById(ID).style.backgroundColor="#fffff"; ?? ??? ??? ??? ?}) ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? methods: { ? ? ? ? ? ? dataChild(){ ? ? ? ? ? ? ? ? //執(zhí)行其他邏輯 ? ? ? ? ? ? } ? ? ? ? }, ? ? ? ? mounted() { ? ? ? ? ? ? //在created、mounted這樣的生命周期, 給 this.data賦值會失敗,錯誤賦值方法 ? ? ? ? ? ? // this.data= this.name;? ? ? ? ? } }
props總結(jié)
獲取不到props的原因:
因為父組件中要傳遞給子組件的 props 屬性 是通過 ajax請求獲取的, 請求的這個過程是需要時間的異步獲取等待返回,然而子組件的渲染要快于ajax請求過程,所以此時在created 、 mounted 只執(zhí)行一次的生命鉤子函數(shù)中,執(zhí)行完成后,此時 props 還沒有傳遞(子組件),所以只能獲取默認(rèn)的props值,當(dāng)props獲取ajax完成后傳遞進(jìn)來,此時生命周期函數(shù)已經(jīng)執(zhí)行完成。所以wacth監(jiān)聽數(shù)據(jù)變化來解決問題。
最后
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Ant?Design?Vue中的table與pagination的聯(lián)合使用方式
這篇文章主要介紹了Ant?Design?Vue中的table與pagination的聯(lián)合使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-10-10vue中this.$router.go(-1)失效(路由改變了,界面未刷新)
本文主要介紹了vue中this.$router.go(-1)失效(路由改變了,界面未刷新),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-12-12Vue 打包的靜態(tài)文件不能直接運(yùn)行的原因及解決辦法
這篇文章主要介紹了Vue 打包的靜態(tài)文件不能直接運(yùn)行的原因及解決辦法,幫助大家更好的理解和學(xué)習(xí)vue框架,感興趣的朋友可以了解下2020-11-11vue實現(xiàn)動態(tài)表單動態(tài)渲染組件的方式(1)
這篇文章主要為大家詳細(xì)介紹了vue實現(xiàn)動態(tài)表單動態(tài)渲染組件的方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-04-04