微信小程序(四十二)wechat-http拦截器

注释很详细,直接上代码

上一篇

新增内容:
1.wechat-http请求的封装
2.wechat-http请求的拦截器的用法演示

源码:

utils/http.js

js 复制代码
import http from "wechat-http"


//设置全局默认请求地址
http.baseURL = "https://live-api.itheima.net"

//设置响应拦截器
http.intercept.response=(res)=>{
    //返回数据格式(减少一层data)
    return res.data
}

//导出
export default http

app.js

js 复制代码
//从utils导入http
import http from './utils/http'

//注册到全局wx实例中
wx.http=http
App({
 globalData:{
     token:''
 }
})

index.js

js 复制代码
Page({
  data:{
    list:[]
  },
  //此处就是第四十和四十一篇的结合,抽离出来了网络请求的调用部分进行封装
  //并且设置了响应拦截器对数据进行前提处理
    async onHttp(){
     const res=await wx.http({
        method:'GET',//类型
        url:'/announcement'//设置了基本网站地址就能这么用
      })

      this.setData({//录入数据
        list:res.data
      })
     
    }

  
})

效果演示:

相关推荐
00后程序员张18 小时前
python 抓包在实际项目中的合理位置,结合代理抓包、设备侧抓包与数据流分析
android·ios·小程序·https·uni-app·iphone·webview
2501_915918411 天前
使用 HBuilder 上架 iOS 应用时常见的问题与应对方式
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
iOS 崩溃日志的分析方法,将崩溃日志与运行过程结合分析
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
React Native 混淆在真项目中的方式,当 JS 和原生同时暴露
javascript·react native·react.js·ios·小程序·uni-app·iphone
00后程序员张1 天前
苹果应用商店上架App流程,签名证书、IPA 校验、上传
android·ios·小程序·https·uni-app·iphone·webview
2501_916007471 天前
iOS 上架需要哪些准备,围绕证书、描述文件和上传方式等关键环节展开分析
android·ios·小程序·https·uni-app·iphone·webview
qq_12498707531 天前
基于微信小程序的私房菜定制上门服务系统(源码+论文+部署+安装)
java·spring boot·微信小程序·小程序·毕业设计·毕设
2501_915106321 天前
iOS 上架费用解析,哪些成本可以通过流程优化降低。
android·ios·小程序·https·uni-app·iphone·webview
换日线°1 天前
微信小程序找不同游戏(有效果图)
游戏·微信小程序
风月歌1 天前
小程序项目之超市售货管理平台小程序源代码(源码+文档)
java·微信小程序·小程序·毕业设计·源码