vue axiox网络请求
一、首先安装axios ,vue-axios
前提:搭建一个vue3的项目
项目搭建参考:https://www.cnblogs.com/yclh/p/15356171.html
使用npm安装axios
npm install axios
npm install vue-axios
二、文件结构
文件说明:
vue.config.js:为了解决跨域问题添加的文件。
network->config.js:配置网络接口中的公共设置
network->http.js:创建axios的实例instance,并编写get、post的通用方法,这里还设置了请求拦截器和相应拦截器。
network->index.js:设置具体调用的接口路径和提供给App.vue的方法。
注:这里要的接口路径是后端提供的可以访问的get和post接口。
例如:/xyresourcesinfot/xyResourcesInfoT/list 这个路径是后台提供的。
App.vue:页面展示为了调用http网络接口效果。

三、效果
分别展示:
1、 不带参数的get请求。
2、 带参数的get请求。
3、 form-data形式的post请求


四、源码:
vue.config.js
//创建vue.config.js文件
module.exports = {
lintOnSave: false, //是否在保存时检查
devServer: {
host: 'localhost', //本机ip
port: 8080, //本机端口
open: true,
proxy: {
'/api': { //代理别名
target: 'http://192.168.0.8:8081', //代理目标值
changeOrigin: true,
secure: true,
pathRewrite: { //替换路径中的/api
'^/api': ''
}
}
},
overlay: {
warning: false,
errors: true
}
}
}
config.js
export default {
baseUrl: {
dev: "/api/xy", // 开发环境
changeOrigin: true,
},
};
http.js
import axios from "axios"; // 引用axios
import qs from 'qs';
import config from "@/network/config"; const instance = axios.create({
baseURL: config.baseUrl.dev,
timeout: 60000, // post 使用form-data
transformRequest: [function(data) {
data = qs.stringify(data);
return data;
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}); //get请求
export function get(url, params = {}) {
// console.log('22222')
// console.log(params)
return new Promise((resolve, reject) => {
instance.get(url, {
params: params,
})
.then((response) => {
resolve(response);
})
.catch((err) => {
reject(err);
});
});
}
//post请求
export function post(url, data = {}) {
return new Promise((resolve, reject) => {
instance.post(url, data).then(
(response) => {
resolve(response.data);
},
(err) => {
reject(err);
}
);
});
} //-------------------- axios的拦截器----------------------
// 1.请求拦截的作用
instance.interceptors.request.use(config => {
console.log('进入请求拦截' );
// 1.比如config中的一些信息不符合服务器的要求
// 2.比如每次发送网络请求时, 都希望在界面中显示一个请求的图标
// 3.某些网络请求(比如登录(token)), 必须携带一些特殊的信息
return config
}, err => {
// console.log(err);
}) // 2.响应拦截
instance.interceptors.response.use(res => {
console.log('进入相应拦截');
return res
}, err => {
console.log(err);
}) // 请求拦截器 post 使用form-data
// instance.interceptors.request.use(
// config => {
// config.data = qs.stringify(config.data) // 转为formdata数据格式
// return config
// },
// error => Promise.error(error)
// )
index.js
import { get,post } from "@/network/http";
export const getResourcesMany = (params) => get("/xyresourcesinfot/list",params);
export const getResourcesSingle = (params) => get("/xyresourcesinfot/get/",params);
export const PostUserInfoT = (data) => post("/xyuserinfot/add",data);
App.vue
<template>
<div>
<div>{{noParams}}</div>
<button @click="getNoParams">get请求网络数据 不带参数</button>
<h2>-----------------------------------</h2>
<div>{{haveParams}}</div>
<!-- <button @click="getHaveParams({ 'smallType': '103', 'needPoint': '100' } )">get请求网络数据 带参数</button> --> <button @click="getHaveParams">get请求网络数据 带参数</button>
<h2>-----------------------------------</h2> <div>{{postResult}}</div>
<button @click="PostHaveParams">Post请求网络数据 带参数</button>
<h2>-----------------------------------</h2>
</div>
</template> <script>
import { computed,ref } from 'vue'
import { useStore } from 'vuex'
import { getResourcesMany,PostUserInfoT } from '@/network/index.js'; export default {
components: {},
setup() {
let noParams = ref('get没有参数');
let haveParams = ref('get有参数');
let postResult = ref('post演示数据'); let getNoParams = () => {
getResourcesMany()
.then(res => {
console.log(res.data.obj.dataList[0]);
noParams.value = res.data.obj.dataList[0];})
.catch(error => { });
} let params = {
smallType : '103',
needPoint : '100'
} let getHaveParams = () => {
getResourcesMany(params)
.then(res => {
console.log(res.data.obj.dataList[0]);
haveParams.value = res.data.obj.dataList[0];})
.catch(error => { });
} let paramsData = {
userEmail: 'yangcheng8517@163.com',
userPassword: 'yangcheng123412312356$abc',
otherName: 'yc'
} let PostHaveParams = () => {
console.log('1111');
console.log(paramsData);
PostUserInfoT(paramsData)
.then(res => {
console.log(res);
postResult.value = res})
.catch(error => { });
} return {
noParams ,haveParams,
getNoParams,getHaveParams,PostHaveParams,
params ,paramsData,postResult
}
} // methods: {
// getCompany() {
// getData()
// .then(res => {
// //console.log(res.data);
// console.log(res.data.obj.dataList[0]);
// 这里不是响应式的
// this.result.value = res.data.obj.dataList[0];})
// .catch(error => { });
// }
// } }
</script>
vue axiox网络请求的更多相关文章
- vue vue-resource网络请求
在使用get/post 网络请求,需要下载插件 "vue-resource" npm install vue-resource -s 在路由要导入及注册 import Vue fr ...
- Vue 网络请求
Vue网络请求,用的是vue-resource 1. 首先需要安装vue-resource npm install vue-resource 2. 安装好之后,会在package.json文件中自动加 ...
- vue使用vue-resource,进行网络请求
首先使用vue-resource,需要安装 : https://blog.csdn.net/qq_36947128/article/details/72832977 下面我写的一个例子: 网络请求应该 ...
- Spring Boot + Vue 前后端分离开发,前端网络请求封装与配置
前端网络访问,主流方案就是 Ajax,Vue 也不例外,在 Vue2.0 之前,网络访问较多的采用 vue-resources,Vue2.0 之后,官方不再建议使用 vue-resources ,这个 ...
- vue(24)网络请求模块axios使用
什么是axios Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.js 中. 主要的作用:axios主要是用于向后台发起请求的,还有在请求中做更多是可控功能. a ...
- 十. Axios网络请求封装
1. 网络模块的选择 Vue中发送网络请求有非常多的方式,那么在开发中如何选择呢? 选择一:传统的Ajax是基于XMLHttpRequest(XHR) 为什么不用它呢?非常好解释配置和调用方式等非常混 ...
- 使用axios优雅的发起网络请求
原文链接:https://www.jianshu.com/p/73585303fdc0 公司项目使用了vue作为技术栈,便理所应当地使用了官方推荐的axios进行网络请求,这里记录下axios的封装方 ...
- Axios 网络请求组件封装 (鉴权、刷新、拦截)
一.前言 注意:本教程需要你对axios有一定的了解,不适用于小白(只能借鉴,希望你能自己动手),注释都写的很清楚.此封装并非完整版,已进行部分删减修改操作,但仍然适用于大部分业务场景,如果不适用于你 ...
- uni-app之网络请求
uni-app之网络请求 一,介绍 uni.request(OBJECT),发起网络请求,以下主要是一些特殊的参数说明,详细的可查看uni-app官网. 1,method的有效值必须是大写,默认GET ...
- vue中异步请求渲染问题(swiper不轮播)(在开发过程中遇到过什么问题、踩过的坑)
问题描述: 用vue封装一个swiper组件的时候,发现轮播图不能轮播了. 原因: 异步请求的时间远大于生命周期执行的时间,mounted初始化DOM时数据未返回,渲染数据是空数组,导致轮播图的容器层 ...
随机推荐
- MINA框架
一.小程序MINA框架分为三个部分: 有 View(视图层).App Service(逻辑层)和 Natice(系统层). 1.View(视图层) 视图层包含了小程序多个页面.每个页面都有WXML文件 ...
- C++ Qt开发:字符串QString容器
在Qt框架中,QString 是一个强大而灵活的字符串容器,专为处理 Unicode 字符而设计.它提供了许多方便的方法来操作和处理字符串,使得在跨平台开发中能够轻松地进行文本操作.QString 是 ...
- [GDOIpj222B] 网页浏览
第二题 网页浏览 提交文件: webpage.cpp 输入文件: webpage.in 输出文件: webpage.out 时间空间限制: 1 秒, 256 MB 我们在上网时,从一个网页上的链接打开 ...
- Git提交修正
应用场景 日常开发中我们可能会遇到这样的问题 1.提交了代码有错误 2.提交的信息写错了 3.漏了一些文件没有提交 ...... 再或者我们写一个功能时,中间有很多小的提交,这中间就会产生特别多的co ...
- python tkinter 使用(六)
python tkinter 使用(六) 本文主要讲述tkinter中进度条的使用. 1:确定的进度条 progressbar = tkinter.ttk.Progressbar(root, mode ...
- R6900 R7000刷梅林 AImesh组网
本文作者: Colin本文链接: https://www.colinjiang.com/archives/netgear-r6900-flash-merlin-rom.html 然后开始讲正题,刷梅林 ...
- P5137 题解
前言 首先感谢所有帮助我卡常的大佬们. 题意 求 \((\sum_{i = 0}^{n} a^i b^{n - i})\mod p\) 的值(\(n \leq 10^{18}\),\(p\) 不一定为 ...
- [极客大挑战 2019]Havefun 1
[极客大挑战 2019]Havefun 1 一,审题,观察题目信息和知识点 观察题目,没发现有效信息 F12打开源代码,发现有一个GET传输. 知识点 GET方法的数据传输是通过URL传输的, ...
- linux中nginx下载安装部署
反向代理 动静结合 负载均衡 下载 wget https://nginx.org/download/nginx-1.24.0.tar.gz 官网下载稳定版 解压 tar -zxvf nginx-1.2 ...
- 云图说 | 图解制品仓库服务CodeArts Artifact
本文分享自华为云社区<[云图说]第277期 图解制品仓库CodeArts Artifact>,作者:阅识风云. 制品仓库服务CodeArts Artifact用于存放源码编译生成的.可运行 ...