微信小程序的接口请求

  • 2019 年 11 月 5 日
  • 筆記

Page({      /**     * 页面的初始数据     */    data: {      message: "数据",      sliderList : [],      },      /**     * 生命周期函数--监听页面加载     */    onLoad: function (options) {      // 常用      // options  用于存放所有的参数  //   数据由 onLoad中存放到 data中  用this.setData();      // this.cardId = options.cat        wx.setNavigationBarTitle({        // title: options.title,      });      //  在加载阶段请求数据      wx.request({        url: 'https://locally.uieee.com/slides',        data: {},        header: {},        //  一定要大写        method: "GET",        // 请求返回的类型        dataType:"json",        // success: function (res) {        //   console.log(res);          // },        success: (res) => {          console.log(res);          // 更新数据  但是没有更新视图            // this.data.sliderList = res.data;            // 所以   但是,这个setData 会更新数据,也会更新视图            //   采用连接数组的形式            var newList = this.data.sliderList.concat(res.data);            this.setData({              sliderList: newList,              })          console.log(this.data.sliderList)          },        // 请求失败        fail: function() {          },        // 请求完成        complete: function () {          }          })              },      /**     * 生命周期函数--监听页面初次渲染完成     */    onReady: function () {      //  弹窗业务      //  这个函数在这个js中第三个触发      },      /**     * 生命周期函数--监听页面显示     */    onShow: function () {      },      /**     * 生命周期函数--监听页面隐藏     */    onHide: function () {      },      /**     * 生命周期函数--监听页面卸载     */    onUnload: function () {      // 内页返回到首页  那么内页就会卸载.作用: 节省空间      },      /**     * 页面相关事件处理函数--监听用户下拉动作     */    onPullDownRefresh: function () {      },      /**     * 页面上拉触底事件的处理函数     */    onReachBottom: function () {      console.log("上拉触底") ;      //  页码加一      // ++this.pageIndex      },      /**     * 用户点击右上角分享     */    onShareAppMessage: function () {      }  })