当前位置:   article > 正文

Axios在vue项目中的基本用法_vue axios jsoncreate

vue axios jsoncreate

参考:https://blog.csdn.net/qq_39765048/article/details/117688019
原文更精辟,请支持原文。

1、Axios简介

axios框架全称(ajax – I/O – system)

  • 基于promise用于浏览器和node.js的http客户端,因此可以使用Promise API

2、axios是做什么的?

是用来实现“异步网络请求”的。

旧浏览器页面在向服务器请求数据时,因为返回的是整个页面的数据,页面都会强制刷新一下,这对于用户来讲并不是很友好。并且我们只是需要修改页面的部分数据,但是从服务器端发送的却是整个页面的数据,十分消耗网络资源。而我们只是需要修改页面的部分数据,也希望不刷新页面,因此异步网络请求就应运而生了。但是这里我们最开始使用的是Ajax

Ajax(Asynchronous JavaScript and XML): 异步网络请求。Ajax能够让页面无刷新的请求数据。

实现ajax的方式有多种,如jQuery封装的ajax,原生的XMLHttpRequest,以及axios。

利弊:

  • 原生的XMLHttpRequest的配置和调用方式都很繁琐,实现异步请求十分麻烦
  • jQuery的ajax相对于原生的ajax是非常好用的,但是没有必引用jQuery框架

Axios(ajax i/o system): 本质上还是对原生XMLHttpRequest的封装,可用于浏览器和nodejs的HTTP客户端,只不过它是基于Promise的,符合最新的ES规范。

特点:

  • 在浏览器中创建XMLHttpRequest请求
  • 在node.js中发送http请求
  • 支持Promise API
  • 拦截请求和响应
  • 转换请求和响应数据
  • 取消要求
  • 自动转换JSON数据
  • 客户端支持防止CSRF/XSRF(跨域请求伪造)

3、安装使用

(1)安装

npm install axios
  • 1

(2)引用

main.js

import axios from 'axios'
Vue.prototype.$axios = axios
  • 1
  • 2

(3)使用axios

<script>
	export default {
		mounted(){
			this.$axios.get('/xxxx').then(res=>{
				console.log(res.data);
			})
		}
	}
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

一般不会上面使用,大多会进行模块封装(详见其他Axios封装文章)

4、Axios请求方式

get post put patch delete
  • 1

5、并发请求

 this.$axios.all([
	this.$axios.get('/goods.json'),
	this.$axios.get('/classify.json')
]).then(
	this.$axios.spread((goodsRes,classifyRes)=>{
		console.log(goodsRes.data);
		console.log(classifyRes.data);
	})
)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

6、Axios实例

(1)创建axios实例

let instance = this.$axios.create({
				baseURL: 'http://localhost:9090',
				timeout: 2000
			})
			
instance.get('/xxxx').then(res=>{
	console.log(res.data);
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

配置说明:

  • baseURL 请求的域名,基本地址,类型:String
  • timeout 请求超时时长,单位ms,类型:Number
  • url 请求路径,类型:String
  • method 请求方法,类型:String
  • headers 设置请求头,类型:Object
  • params 请求参数,将参数拼接在URL上,类型:Object
  • data 请求参数,将参数放到请求体中,类型:Object

(2)axios全局配置

//配置全局的超时时长
this.$axios.defaults.timeout = 2000;
//配置全局的基本URL
this.$axios.defaults.baseURL = 'http://localhost:8080';
  • 1
  • 2
  • 3
  • 4

(3)axios实例配置

let instance = this.$axios.create();
instance.defaults.timeout = 3000;
  • 1
  • 2

(4)axios请求配置

this.$axios.get('/goods.json',{
				timeout: 3000
			}).then()
  • 1
  • 2
  • 3

优先级为:请求配置 > 实例配置 > 全局配置

7、拦截器

(1)请求拦截器

this.$axios.interceptors.request.use(config=>{
				// 发生请求前的处理

				return config
			},err=>{
				// 请求错误处理

				return Promise.reject(err);
			})

//或者用axios实例创建拦截器
let instance = $axios.create();
instance.interceptors.request.use(config=>{
    return config
})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

(2)响应拦截器

this.$axios.interceptors.response.use(res=>{
				//请求成功对响应数据做处理

				return res //该返回对象会传到请求方法的响应对象中
			},err=>{
				// 响应错误处理

				return Promise.reject(err);
			})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

(3)取消拦截

let instance = this.$axios.interceptors.request.use(config=>{
				config.headers = {
					token: ''
				}
				return config
			})
			
//取消拦截
this.$axios.interceptors.request.eject(instance);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

8、错误处理

this.$axios.get('/url').then(res={

			}).catch(err=>{
				//请求拦截器和响应拦截器抛出错误时,返回的err对象会传给当前函数的err对象
				console.log(err);
			})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

9、取消请求

let source = this.$axios.CancelToken.source();

this.$axios.get('/goods.json',{
				cancelToken: source
			}).then(res=>{
				console.log(res)
			}).catch(err=>{
				//取消请求后会执行该方法
				console.log(err)
			})

//取消请求,参数可选,该参数信息会发送到请求的catch中
source.cancel('取消后的信息');
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

结束。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/465998
推荐阅读
相关标签
  

闽ICP备14008679号