问题描述
根据wepy文档中写的request方法出不来,代码如下:
methods = {
request() {
let that = this
wepy.request({
url: url,
data: 'data',
method: 'GET',
dataType: 'json',
success: res => {
console.log(res.data)
res.data.list.forEach(function (d) {
d.time = that.getTimeUntilNow(d.time)
})
console.log(res.data)
that.list = res.data.list
this.$apply()
},
fail: () => {},
complete: () => {
}
})
}
}
onLoad() {
this.request()
}
解决方法
wepy框架文档中有云:
注意,对于WePY中的methods属性,因为与Vue中的使用习惯不一致,非常容易造成误解,这里需要特别强调一下:WePY中的methods属性只能声明页面wxml标签的bind、catch事件,不能声明自定义方法,这与Vue中的用法是不一致的。
这里request方法是自定义方法,此方法页面加载时就执行,所以需要把request函数提到methods外面去,与methods平级,就可以了。如果此方法是绑定在@tap事件里面,就可以把此方法放在methods里面了。解决代码如下:
methods = {
}
request() {
let that = this
wepy.request({
url: url,
data: 'data',
method: 'GET',
dataType: 'json',
success: res => {
console.log(res.data)
res.data.list.forEach(function (d) {
d.time = that.getTimeUntilNow(d.time)
})
console.log(res.data)
that.list = res.data.list
this.$apply()
},
fail: () => {},
complete: () => {
}
})
}
onLoad() {
this.request()
}