vue 中使用axios 的方式
第一种
定义axios函数
a 文件
import axios from "axios";
export default axios.create({
baseURL:'接口',
// headers:{'content-type':'application/json'}
// application/x-www-form-urlencoded
})b 文件调用
import axios from "./axios";
export const colum = (obj)=>{
return axios.post('子接口',{
参数
})
}vue 文件 处理函数返回参数
colum({ 参数 }).then(res => {
处理返回数据
});第二种
定义函数
pub.js
import $ from "jquery";
import axios from 'axios'
_InitAxios(op) {
axios({
url: op._url + op.ur,
method: "post",
data: JSON.stringify(op.data),
headers: {
"Content-Type": "application/json"
}
})
.then(res => {
console.log('axios 访问返回数据', res)
op.cbk(res.data);
})
.catch(cat => {});
}vue 文件调用
import $ from "jquery";
import axios from "axios";
import { pub } from "./assets/js/pub";
var op_data = {
that: that,
_url: ,// 总接口
ur: , // 具体接口
cbk: that.cb, // 回调
data: { } // 形参
};
pub._InitAxios(op_data);
cb(res){
处理返回参数
}
随时随地看视频