stohk
stohk
  • 发布:2019-11-21 11:08
  • 更新:2019-11-21 11:08
  • 阅读:2044

使用自定义组件时,向子组件动态传值,数据会不更新

分类:uni-app

主页上通过网络请求获取动态数据,然后传给子组件显示出来,发觉uni.request是异步的,导致子组件接收不到数据,我在群里问了一下,说要什么回调,新手实在不懂这该怎么写,哪位大神能帮我看一下么?
<template>
<view>
<sho :wto='ggg'></sho>

</template>

<script>
import sho from '../../components/sho.vue'
export default {
data() {
return {
ggg:[{id:1,name:'111'},{id:2,name:'www'},{id:3,name:'hhhh'}]
}
},
onLoad(){
var that = this;
this.doww(14); //这里通过函数获取网络数据更新data里面的ggg数组
console.log(that.ggg); //但这里的值还是原值
},
methods: {
doww(s){
uni.showLoading({
title: '加载中',
mask:true
});
var requestUrl = 'http://192.168.10.69' + '/pag.php';
console.log(requestUrl);
uni.request({
url: requestUrl,
header: {'Content-Type': 'application/x-www-form-urlencoded'},
method:"POST",
data: {
pag:s
},
success: (res) => {

                            console.log('请求成功!')  
                            if (s<=14){  
                             this.ggg = res.data;      //这里更新变量  
                             console.log(this.ggg)  
                            }else{  
                                this.ggg = this.ggg.concat(res.data)  
                            }             
                        },  
                        fail: (err) => {  
                            console.log('请求失败', err);  
                        },  
                    });  
                    setTimeout(function () {  
                    uni.hideLoading();  
                    }, 100);  
        },  
    },  
    components:{  
        sho  
    }  
}  

</script>

2019-11-21 11:08 负责人:无 分享
已邀请:

该问题目前已经被锁定, 无法添加新回复