zoukankan      html  css  js  c++  java
  • mpvue封装axios实现数据的请求

     第一步:下载axios

    npm install --save axios
    第二步:更改axios的文件:路径:
    ode_modulesaxiosdistaxios.js
    //将这段代码隐藏,使用自定义的adapter
    axios.all = function all(promises) {
    	  return Promise.all(promises);
    };
    

     第三步:在utils文件底下创建axios.js文件

    import axios from 'axios'
    import Qs from
    'qs' // 时间戳 // const NewTimeStamp = new Date().getTime() // 定义请求类 class HttpRequest { constructor (baseUrl = baseURL) { this.baseUrl = baseUrl this.queue = {} } getInsideConfig () { const config = { baseURL: this.baseUrl, } return config } // 小程序自定义处理请求 adapter (instance) { instance.defaults.timeout = 30000 instance.defaults.adapter = function (config) { return new Promise((resolve, reject) => { // console.log(config,'adapter') let data = config.method === 'get' ? config.params : Qs.stringify(Object.assign(JSON.parse(config.data),vertifyData())) // wx小程序 发起请求相应 log 就可以看到熟悉的返回啦 // console.log(typeof config.data, config, data) wx.request({ url: config.url, method: config.method, data: data, header: { 'content-type': 'application/x-www-form-urlencoded;charset=UTF-8' // 默认值 }, success: (res) => {
            return resolve(res.data)
                },
              fail: (err) => { return reject(err) }
            })
          })
        }
      }
      distroy (url) {
        delete this.queue[url]
        if (!Object.keys(this.queue).length) {
          // Spin.hide()
        }
      }
      // 拦截器
      interceptors (instance, url) {
        // 请求拦截
        instance.interceptors.request.use(config => {
          // 添加全局的loading...
          if (!Object.keys(this.queue).length) {
            // Spin.show() // 不建议开启,因为界面不友好
          }
          this.queue[url] = true
          // 统一拦截添加token
          config.params = {}
          // config.params.token = getToken()
          return config
        }, error => {
          return Promise.reject(error)
        })
        // 响应拦截
        instance.interceptors.response.use(res => {
          this.distroy(url)
          const { data, status } = res
          // 接口token过期处理
          // if (res.data.Code === -202) {
          //   router.push({ name: 'login', params: { failure: true } })
          //   // return Promise.reject(new Error(res.data.Message).message)
          // }
          return { data, status }
        }, error => {
          this.distroy(url)
          return Promise.reject(error)
        })
      }
      // 请求开始
      request (options) {
        const instance = axios.create()
        const configInfo = this.getInsideConfig()
        this.adapter(instance)
        options.url = this.getInsideConfig().baseURL + options.url
        options = Object.assign(this.getInsideConfig(), options)
        this.interceptors(instance, options.url)
        return instance(options)
      }
    }
    export default HttpRequest
    第四步:在utils文件底下创建request.js文件
    import HttpRequest from '@/utils/axios'
    const configBaseUrl={
      dev: 'https://www.****.com',
      pro: 'http://127.0.0.1:8080'
    }
    const baseUrl = process.env.NODE_ENV === 'development' ? configBaseUrl.dev : configBaseUrl.pro
    
    const axios = new HttpRequest(baseUrl)
    export default axios
    第五步:使用axios请求数据
    //mobileGetCourseList是请求的地址,例如/api/web/course/list
    axios.request({
          url: mobileGetCourseList,
          method: 'post',
          data: {},
        }).then(response => {
          console.log(response)
        })
  • 相关阅读:
    socket bind详解
    Hibernate简单的基础理论
    Web的工作机制
    部署hibernate框架项目时出现问题:The type java.lang.Object cannot be resolved. It is indirectly referenced from required .class files.
    Spring简单的小例子SpringDemo,用于初略理解什么是Spring以及JavaBean的一些概念
    Hibernate项目里配置环境时,jar包配置不当会对测试结果产生影响。
    转载——Struts2中的constant详解
    同样的WiFi,手机能连上网,电脑不能。错误代码DNS_PROBE_POSSIBLE
    eclipse外部导入Javaweb项目时,项目上出现红叉的一个可能的解决办法
    依赖注入的实现方式:设值注入和构造方法注入
  • 原文地址:https://www.cnblogs.com/liucailing/p/13153751.html
Copyright © 2011-2022 走看看