微信小程序的新的

app.request.get('http://ele.kassing.cn/v1/pois',this.data.city).then(res=>{
      console.log(res)
      this.setData({
        addr:res.data
      })
    }),

在app.js中定义全局方法

  request: {
    get(url, data = {}) {
      return new Promise((resolve, reject) => {
        // 进行请求
        wx.request({
          url,
          data,
          success: (res) => {
            resolve(res)
          },
          fail(err) {
            reject(err)
          }
        })
      })
    },
    post(url, data = {}) {
      return new Promise((resolve, reject) => {
        wx.request({
          url,
          data,
          method: "POST",
          success: res => {
            resolve(res)
          },
          fail: (err) => {
            reject(err)
          }
        })
      })
    }
  }

 如果在某一个页面需要使用全局的方法,只需要在页面顶部添加代码

const app=getApp();

 正常使用的时候为

app.request.get('http://ele.kassing.cn/v1/pois',this.data.city).then(res=>{
      console.log(res)
      this.setData({
        addr:res.data
      })
    }),

猜你喜欢

转载自www.cnblogs.com/mrxinxin/p/10284289.html