新建页面,封装axios请求;
import axios from "axios";
//创建axios实例
var service = axios.create({
baseURL: "后端接口地址前缀",
timeout: 5000,
withCredentials: true,
headers: {
"content-type": "application/json",
},
});
export default {
service,
//get请求
get(url, data) {
return service({
url: url,
method: "get",
query: data,
});
},
//post请求
post(url, data) {
return service({
url: url,
method: "post",
data: data,
});
},
};
main.js引用封装的请求;
import http from "@/api/http.js"; //axios实例化后引入取名http
Vue.prototype.$http = http; //放入全局
页面使用请求
this.$http.get("后端接口地址前缀的拼接",{data: 1,array: []}).then((res) => {});
————————————————
版权声明:本文为CSDN博主「前端_菜鸟」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jiaodeqiangs/article/details/111477010
网友评论