欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue在mounted拿不到props中傳遞的數(shù)據(jù)問題

 更新時間:2023年03月24日 08:55:32   作者:老大oba  
這篇文章主要介紹了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)文章

最新評論