axios怎么用?

引入:

<!-- 開(kāi)發(fā)環(huán)境版本,包含了有幫助的命令行警告 -->

<script src="https://cdn.jsdelivr.net/npm/vueldist/vue.js"></script>

【最好是使用第一種版本】

或者:

<!-- 生產(chǎn)環(huán)境版本,優(yōu)化了尺寸和速度 -->



第一步:找到你要用得項(xiàng)目得根目錄? ?

2:npm install axios 進(jìn)行一個(gè)下載

?執(zhí)行完后會(huì)發(fā)現(xiàn)uniapp和vue的項(xiàng)目一樣,多了一個(gè)node_module文件夾,文件夾中多了一個(gè)axios文件夾,即安裝成功。

3:

import Vue from 'vue'

import App from './App'import axios from 'axios'

// create an axios instance

const service = axios.create({

? ? baseURL: 'http://192.168.0.105:8090', // url = base url + request url

? ? withCredentials: true, // send cookies when cross-domain requests

? ? // timeout: 5000, // request timeout

? ? crossDomain: true

})

Vue.prototype.$axios = service

4.

import Vuefrom'vue'

export defaultVue.prototype.$axios

5.

axios.defaults.adapter = function(config) {

? ? return new Promise((resolve, reject) => {

? ? ? ? console.log(config)

? ? ? ? var settle = require('axios/lib/core/settle');

? ? ? ? var buildURL = require('axios/lib/helpers/buildURL');

? ? ? ? uni.request({

? ? ? ? ? ? method: config.method.toUpperCase(),

? ? ? ? ? ? url: config.baseURL + buildURL(config.url, config.params, config.paramsSerializer),

? ? ? ? ? ? header: config.headers,

? ? ? ? ? ? data: config.data,

? ? ? ? ? ? dataType: config.dataType,

? ? ? ? ? ? responseType: config.responseType,

? ? ? ? ? ? sslVerify: config.sslVerify,

? ? ? ? ? ? complete: function complete(response) {

? ? ? ? ? ? ? ? response = {

? ? ? ? ? ? ? ? ? ? data: response.data,

? ? ? ? ? ? ? ? ? ? status: response.statusCode,

? ? ? ? ? ? ? ? ? ? errMsg: response.errMsg,

? ? ? ? ? ? ? ? ? ? header: response.header,

? ? ? ? ? ? ? ? ? ? config: config

? ? ? ? ? ? ? ? };

? ? ? ? ? ? ? ? settle(resolve, reject, response);

? ? ? ? ? ? }

? ? ? ? })

? ? })

}


關(guān)于axios在h5中做的一個(gè)demo

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

<title></title>

<style>

body {

background: url(img/1.png) no-repeat center center fixed;

background-size: 100%;

}

</style>

</head>

<body>

<input class="get" type="button" value="ni" />

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

<script>

document.querySelector(".get").onclick=function(){

axios.get("https://autumnfish.cn/api/joke").then(function(response){

console.log(response);

})

}

</script>

</body>

</html>

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容