プログラム版はpromiseのaxiosスタイルajaxリクエスト関数と結合しています.


提案されたウィジェットバージョンのaxios関数は,単純といえば,通常のリクエストメソッドの外にブロックを追加しただけである.具体的には以下の通りです.
ダウンロード、インストール
npm i cdd-lib

使用
es6:
import {wxhttp} from 'cdd-lib'

commonjs:
let {wxhttp} = require('cdd-lib')

wxhttp wxhttpと命名
具体的なリクエストの使い方はaxiosなどです
wxhttp#request(config)
wxhttp#get(url[,config])
wxhttp#delete(url[,config])
wxhttp#head(url[,config])
wxhttp#options(url[,config])
wxhttp#post(url[,data[,config]])
wxhttp#put(url[,data[,config]])
wxhttp#patch(url[,data[,config]])
ブロック方法:
  • 要求ブロック
  • wxhttp.interceptors.request.use(handleRequest(config),handleError(err))
    注意:handleRequestは処理後のconfigを返す必要があります
  • はブロック
  • に戻る.
    wxhttp.interceptors.response.use(handresponse(res))
    注意:handleResponseは処理後のresを返す必要があります.
    例:
    import $http from "../../utils/http"
    export default {
      name: 'seckillHome',
      data() {
        return {
    
        }
      },
      onShow() {
        //     
        $http.interceptors.request.use(function (config) {
          console.log(`    `, config)
          //                   ,             。
          config.data = {
            address: "      "
          }
          return config
        })
    
        $http.post('https://www.baidu.com', {
          name: 'cdd',
          age: 23
        }).then(res => {
          console.log(`   `, res)
        })
      }
    }

    Promiseスタイルを使用しているので、Promise.allメソッドを使用して同時リクエストを行うことができます.
    ソースの表示