vue axios
vue2.0之axios接口請求管理
功能特性
axios API
開始使用
get請求
post请求
多个请求并发
拦截器
移除一个拦截器:
自定义的 axios 实例添加拦截器:
vue2.0之axios接口請求管理
基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用

Vue 原本有一个官方推荐的 ajax 插件 vue-resource,但是自从 Vue 更新到 2.0 之后,尤雨溪宣布停止更新vue-resource,并推荐大家使用axios之后,越来越多的 Vue 项目,都选择 axios 来完成 ajax 请求,而大型项目会使用 Vuex 来管理数据

功能特性
1.在浏览器中发送 XMLHttpRequests 请求
2.在 node.js 中发送 http请求
3.支持 Promise API
4.拦截请求和响应
5.转换请求和响应数据
6.自动转换 JSON 数据
7.客户端支持保护安全免受 XSRF 攻击

axios API
axios.get(url[, config])
axios.delete(url[, config])
axios.head(url[, config])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])

注意
当使用别名方法时, url、 method 和 data 属性不需要在 config 参数里面指定。

開始使用
vue 本身是不支持 ajax 接口请求的,所以我们需要安装一个接口请求的 npm 包,来使我们的项目拥有这个功能。
way1.首先在主入口文件main.js中引用

import axios from 'axios'
import VueAxios from 'vue-axios' Vue.use(VueAxios,axios);

  在组件文件中的methods里去使用

this.axios.get('api/getNewsList').then((response)=>{
this.newsList=response.data.data;
}).catch((response)=>{
console.log(response);
})

  way2.axios 改写为 Vue 的原型属性
首先在主入口文件main.js中引用,之后挂在vue的原型链上

import axios from 'axios'
Vue.prototype.$ajax= axios

  在组件中使用

this.$ajax.get('api/getNewsList').then((response)=>{
this.newsList=response.data.data;
}).catch((response)=>{
console.log(response);
})

  way3.结合 Vuex的action ,在vuex的仓库文件store.js中引用,使用action添加方法

import Vue from 'Vue'
import Vuex from 'vuex' import axios from 'axios' Vue.use(Vuex)
const store = new Vuex.Store({
// 定义状态
state: {
user: {
name: 'xiaoming'
}
},
actions: {
// 封装一个 ajax 方法
login (context) {
axios({
method: 'post',
url: '/user',
data: context.state.user
})
}
}
}) export default store

在组件中发送请求的时候,需要使用 this.$store.dispatch

methods: {
submitForm () {
this.$store.dispatch('login')
}
}

  

get請求

写法如下:

axios.get('/detail?id=10').then(function (res) {
//成功获取数据
console.log(res);
}).catch(function (err) {
//请求错误
console.log(err);
});

  

get请求也可以通过 params 对象传递参数。写法如下:

 axios.get('/detail', {
//参数
params: {
id: 10
}
}).then(function (res) {
//成功获取数据
console.log(res);
}).catch(function (err) {
//请求错误
console.log(err);
});

  

post请求

写法如下:

/执行post请求
axios.post('/add', {
name: 'haqiu',
age: 26
}).then(function (res) {
//请求成功
console.log(res);
}).catch(function (err) {
//请求失败
console.log(err);
});

  

多个请求并发

除了最常用的get请求和post请求以外,值得一提的是axios提供了一次性并发多个请求的API,使用方法如下:

function getProfile(){
//请求1
return axios.get('/profile');
}
function getUser(){
//请求2
return axios.get('/user');
}
//并发请求
axios.all([
getProfile(),
getUser()
]).then(axios.spread((res1, res2)=>{
//两个请求现已完成
console.log(res1);
console.log(res2);
}));

  

拦截器

你可以在处理 then 或 catch 之前拦截请求和响应

// 添加一个请求拦截器
axios.interceptors.request.use(function (config) {
// Do something before request is sent
return config;
}, function (error) {
// Do something with request error
return Promise.reject(error);
}); // 添加一个响应拦截器
axios.interceptors.response.use(function (response) {
// Do something with response data
return response;
}, function (error) {
// Do something with response error
return Promise.reject(error);
});

  

移除一个拦截器:

var myInterceptor = axios.interceptors.request.use(function () {/*...*/});
axios.interceptors.request.eject(myInterceptor);

  

自定义的 axios 实例添加拦截器:

var instance = axios.create();
instance.interceptors.request.use(function () {/*...*/});

  

vue axios----基于 Promise 的 HTTP 请求的更多相关文章

  1. axios - 基于 Promise 的 HTTP 异步请求库

    axios 是基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用.Vue 更新到2.0之后,作者就宣告不再对 vue-resource 模块更新,而是推荐使用 a ...

  2. 基于 Promise 的 HTTP 请求客户端 axios

    基于 Promise 的 HTTP 请求客户端,可同时在浏览器和 node.js 中使用 功能特性 在浏览器中发送 XMLHttpRequests 请求 在 node.js 中发送 http请求 支持 ...

  3. vue --- axios拦截器+form格式请求体

    在vue2.x中使用CLI生成的模板有很大改变,需要自己手动在main.ts同级目录下新建interceptors.ts interceptors.ts import axios from 'axio ...

  4. Vue+axios的四种异步请求,参数的携带以及接收

    Vue中axios发送GET, POST, DELETE, PUT四种异步请求,参数携带和接收问题 web.xml配置如下 1.GET请求 发送GET请求: <!--params是关键字,说明所 ...

  5. Vue(七)发送Ajax请求

    发送AJAX请求 1. 简介 vue本身不支持发送AJAX请求,需要使用vue-resource.axios等插件实现 axios是一个基于Promise的HTTP请求客户端,用来发送请求,也是vue ...

  6. 简单的基于promise的ajax封装

    基于promise的ajax封装 //调用方式: /* ajaxPrmomise({ url:, method:, headers:{} }).then(res=>{}) */ ;(functi ...

  7. Axios 是一个基于 promise 的 HTTP 库

    Axios 是一个基于 promise 的 HTTP 库 vue项目中关于axios的简单使用 axios介绍 Axios 是一个基于 promise 的 HTTP 库,可以用在浏览器和 node.j ...

  8. vue+axios+promise实际开发用法

    axios它是基于promise的http库,可运行在浏览器端和node.js中,然后作者尤雨溪也是果断放弃了对其官方库vue-resource的维护,直接推荐axios库,小编我也是从vue-res ...

  9. 基于promise用于浏览器和node.js的http客户端的axios

    axios 是一个基于Promise 用于浏览器和 nodejs 的 HTTP 客户端,它本身具有以下特征: 从浏览器中创建 XMLHttpRequest 从 node.js 发出 http 请求 支 ...

随机推荐

  1. ubuntu中搭建基本的开发环境

    1.搭建基本开发环境: sudo apt-get install build-essential 2.安装语法.词法分析器 sudo apt-get install bison flex 3.安装C函 ...

  2. Raspberry Pi 4B 安装QT5和qtCreator

    https://blog.csdn.net/coekjin/article/details/52049273 sudo apt-get install qt5-default sudo apt-get ...

  3. 从Airbnb的发展历程和网易云的大起大落看IT行业创新(第5周课后作业)

    我想先根据个人看法回答“创新是什么?”这个空泛的问题.创新是面对当下的资源条件限制创造出能够满足动态需求或解决动态发展中的问题的新策略.这种实用化定义在大部分邻域都勉强能让定义者自圆其说,对于IT行业 ...

  4. window 下总是object_detection/protos/*.proto: No such file or directory

    这是因为目前的protoc3.5有Bug,换成3.4就好了https://github.com/google/protobuf/releases/tag/v3.4.0

  5. fragment中的onCreateView和onViewCreated的区别和

    (1)  onViewCreated在onCreateView执行完后立即执行. (2)  onCreateView返回的就是fragment要显示的view.

  6. mac 命令行

    本文为使用到过的命令行,仅方便自己查阅 1.进入目录文件 cd name name为文件名 2.返回上一级目录 cd ../ 3.创建文件夹 mkdir name 4.删除文件夹(文件夹下不能包含文件 ...

  7. 用JavaScript写一个JD放大镜

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. python基础和编程库

    Python编程从入门到实践-------基础入门 1.Python中的变量 2.Python首字母大写使用title()方法,全部大写upper()方法,全部小写lower()方法 3.Python ...

  9. 【leetcode】969. Pancake Sorting

    题目如下: Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.len ...

  10. Java——接口interface

    3.5接口interface ①有时必须从几个类中派生出一个子类,继承它们所有的属性和方法.但是,Java不支持多重继承.有了接口,就可以得到多重继承的效果. ②接口(interface)是抽象方法和 ...