<script>
//注意:创建页面的时候必须 暴露 、 不然可能会卡死
export default{
data(){
return{
list:[]
}
},
methods:{
requestData(){
var that=this;
var api='http://www.00000.com/appapi.php';
wx.request({
url: api, //仅为示例,并非真实的接口地址
data: {
a: 'getPortalList' ,
catid: '20',
page:'1'
},
header: {
'content-type': 'application/json' // 默认值
},
success: function(res) {
// console.log(res.data)
that.list=res.data.result;
}
})
}
}
/*生命周期函数:可以用vue的 也可以用小程序的*/
created(){
this.requestData();
}
}
</script>
要想把请求成功后回调函数中的res.data赋值给 data(){return{
list:[]}}中的list,需要1.在声明周期函数中写上发送请求所在的那个函数,具体什么原因不清楚,查了好多资料最终的出来的;
/*生命周期函数:可以用vue的 也可以用小程序的*/
created(){
this.requestData();
}
然后在方法中声明一个变量
var that=this;
最后这样赋值,res.data中的数据要根据你的数据结构灵活变化
that.list=res.data.result;