9***@qq.com
9***@qq.com
  • 发布:2020-04-02 11:49
  • 更新:2020-04-11 14:27
  • 阅读:766

封装了ajax方法,方法里的that.data能获取到数据,但是外面那个that.data就为空

分类:uni-app

onload(){
let that = this
jewel.get('xxxx/xxxx', {}, false, function(res){
that.data = res.data.data;
})

console.log(JSON.stringify(that.data))  

}

2020-04-02 11:49 负责人:无 分享
已邀请:
tinyfang

tinyfang

打印两个that对比就知道了吧

  • 9***@qq.com (作者)

    打印了,报错----------

    [Vue warn]: Property or method "toJSON" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

    13:57:29.152 (found in <Root>)

    13:57:29.172 [Vue warn]: Property or method "toJSON" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

    13:57:29.214 (found in <Root>)

    13:57:29.234 [object object], wwww

    2020-04-02 13:58

tinyfang

tinyfang

toJSON函数没定义,我猜也是this指向问题吧。打印日志不用转json,直接打印就好了。

liuxy

liuxy - 承接各种公众号小程序app开发, 前后全包,wx或电话咨询联系18724597563

你打印时, that.data = res.data.data; 还没有执行啊

全盘崩坏

全盘崩坏 - 此颜差矣!

撸代码不先了解点基础知识么, js语言虽然是单线程, 但XMLRequest却是异步的, js处理异步使用的是回调函数, 也就是说你发出了请求后, 你以为会等请求结束后才会执行请求外的方法,实际并不是,发起请求后并不影响写在请求之后的代码执行,要实现在请求返回结果后在继续执行请求后面的代码,请使用async申明函数为异步函数,在配合await实现

  • 9***@qq.com (作者)

    后面撸明白了,哈哈,多谢赐教

    2020-04-15 14:08

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