Event transmission parameters - Encapsulated network requests api

 
 1 ==> passed parameters by clicking on the event
   <view bindtap = "goEdution" data -index = "5"> Southwest University </ view>
  <View bindtap = "goEdution" data-index = "6"> Beijing Normal </ view>
  <View bindtap = "goEdution" data-index = "7"> Chengdu University </ view>


  // parameters passed 
  goEdution (e) {
    the console.log ( "parameters passed over", e.currentTarget.dataset [ 'index']) // . 5. 6. 7 
  },

  When using data transmission parameters - the beginning like
  To accept the parameters passed by the dataset

 

2===
  Package network requests
  Creating serverhttpapi file in the same directory folder page == "Creating httpapi.js file

  export default function mynetwork(options){
  console.log ( "You call me" );
  wx.request({
    URL: options.url, // requested address 
    Method: options.method || "GET", // mode 
    Data: options.data || {}, // parameter 
    // successful callback 
    Success: function (RES) {
      console.log(res)
    },
    fail:function(err){
      console.log ( "call failed" )
    }
  })
}

Since here can not be printed directly using it as promise Note that the return values ​​oh

export default function mynetwork(options){
  return new Promise((resolve,reject)=>{
    wx.request({
      URL: options.url, // requested address 
      Method: options.method || "GET", // mode 
      Data: options.data || {}, // parameter 
      // successful callback 
      Success: function (RES) {
       resolve(res)
      },
      fail: function (err) {
       reject(err)
      }
    })
  })
}


Introduced a page js
import mynetwork from "../../serverhttpapi/httpapi.js"
Page({

})

transfer
  onLoad: function (options) {
    mynetwork({
      url: "https://edu.51cto.com/center/seckill/index/get-seckill-data",
      method: "get",
    }).then(res=>{
      the console.log ( "package", RES) // output data 
    }). the catch (ERR => {
      console.log(err)
    })
  }

 

Guess you like

Origin www.cnblogs.com/IwishIcould/p/11695753.html