プログラム版はpromiseのaxiosスタイルajaxリクエスト関数と結合しています.
1528 ワード
提案されたウィジェットバージョンのaxios関数は,単純といえば,通常のリクエストメソッドの外にブロックを追加しただけである.具体的には以下の通りです.
ダウンロード、インストール
使用
es6:
commonjs:
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を返す必要があります.
例:
Promiseスタイルを使用しているので、Promise.allメソッドを使用して同時リクエストを行うことができます.
ソースの表示
ダウンロード、インストール
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]])
ブロック方法:
注意: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メソッドを使用して同時リクエストを行うことができます.
ソースの表示