1==>通过点击事件进行传递参数
<view bindtap="goEdution" data-index="5">西南大学</view> <view bindtap="goEdution" data-index="6">北京师范</view> <view bindtap="goEdution" data-index="7">成都大学</view> // 传递的参数 goEdution(e) { console.log("传递过来的参数", e.currentTarget.dataset['index']) //5 6 7 }, 传递参数时 使用data-开头就好了 传递的参数通过dataset来接受
2===》
封装网络请求
在page同级目录下创建 serverhttpapi文件夹 ==》创建httpapi.js文件
export default function mynetwork(options){ console.log("你调用了我"); wx.request({ url: options.url, //请求的地址 method:options.method||"get",//方式 data:options.data||{},//参数 // 成功的回调 success:function(res){ console.log(res) }, fail:function(err){ console.log("失败的调用") } }) }
由于不能够直接在这里打印出来 所以使用promise 注意返回值哦 如下
export default function mynetwork(options){ return new Promise((resolve,reject)=>{ wx.request({ url: options.url, //请求的地址 method: options.method || "get",//方式 data: options.data || {},//参数 // 成功的回调 success: function (res) { resolve(res) }, fail: function (err) { reject(err) } }) }) }
在某个js页面引入
import mynetwork from "../../serverhttpapi/httpapi.js" Page({ })
调用
onLoad: function (options) { mynetwork({ url: "https://edu.51cto.com/center/seckill/index/get-seckill-data", method: "get", }).then(res=>{ console.log("封装",res) //输出数据 }).catch(err=>{ console.log(err) }) }