第一步还是先下载axios

  1. cnpm install axios -S

第二步建立一个htttp.js

  1. import axios from 'axios';
  2. import { Message } from 'element-ui';
  3.  
  4. axios.defaults.timeout = 5000;
  5. axios.defaults.baseURL ='';
  6.  
  7. //http request 拦截器
  8. axios.interceptors.request.use(
  9. config => {
  10. // const token = getCookie('名称');注意使用的时候需要引入cookie方法,推荐js-cookie
  11. config.data = JSON.stringify(config.data);
  12. config.headers = {
  13. 'Content-Type':'application/x-www-form-urlencoded'
  14. }
  15. // if(token){
  16. // config.params = {'token':token}
  17. // }
  18. return config;
  19. },
  20. error => {
  21. return Promise.reject(err);
  22. }
  23. );
  24.  
  25. //http response 拦截器
  26. axios.interceptors.response.use(
  27. response => {
  28. if(response.data.errCode ==2){
  29. router.push({
  30. path:"/login",
  31. query:{redirect:router.currentRoute.fullPath}//从哪个页面跳转
  32. })
  33. }
  34. return response;
  35. },
  36. error => {
  37. return Promise.reject(error)
  38. }
  39. )
  40.  
  41. /**
  42. * 封装get方法
  43. * @param url
  44. * @param data
  45. * @returns {Promise}
  46. */
  47.  
  48. export function fetch(url,params={}){
  49. return new Promise((resolve,reject) => {
  50. axios.get(url,{
  51. params:params
  52. })
  53. .then(response => {
  54. resolve(response.data);
  55. })
  56. .catch(err => {
  57. reject(err)
  58. })
  59. })
  60. }
  61.  
  62. /**
  63. * 封装post请求
  64. * @param url
  65. * @param data
  66. * @returns {Promise}
  67. */
  68.  
  69. export function post(url,data = {}){
  70. return new Promise((resolve,reject) => {
  71. axios.post(url,data)
  72. .then(response => {
  73. resolve(response.data);
  74. },err => {
  75. reject(err)
  76. })
  77. })
  78. }
  79.  
  80. /**
  81. * 封装patch请求
  82. * @param url
  83. * @param data
  84. * @returns {Promise}
  85. */
  86.  
  87. export function patch(url,data = {}){
  88. return new Promise((resolve,reject) => {
  89. axios.patch(url,data)
  90. .then(response => {
  91. resolve(response.data);
  92. },err => {
  93. reject(err)
  94. })
  95. })
  96. }
  97.  
  98. /**
  99. * 封装put请求
  100. * @param url
  101. * @param data
  102. * @returns {Promise}
  103. */
  104.  
  105. export function put(url,data = {}){
  106. return new Promise((resolve,reject) => {
  107. axios.put(url,data)
  108. .then(response => {
  109. resolve(response.data);
  110. },err => {
  111. reject(err)
  112. })
  113. })
  114. }

第三步

在main.js中引入

  1. import axios from 'axios'
  2. import {post,fetch,patch,put} from './utils/http'
  3. //定义全局变量
  4. Vue.prototype.$post=post;
  5. Vue.prototype.$fetch=fetch;
  6. Vue.prototype.$patch=patch;
  7. Vue.prototype.$put=put;

最后在组件里直接使用

  1. mounted(){
  2. this.$fetch('/api/v2/movie/top250')
  3. .then((response) => {
  4. console.log(response)
  5. })
  6. },
  7.  
  8. 其余的方法一样

vue中axios的封装的更多相关文章

  1. 聊聊 Vue 中 axios 的封装

    聊聊 Vue 中 axios 的封装 axios 是 Vue 官方推荐的一个 HTTP 库,用 axios 官方简介来介绍它,就是: Axios 是一个基于 promise 的 HTTP 库,可以用在 ...

  2. Vue中axios的封装和api接口的统一管理

    更新的是我csdn上的文章,需要的话可以看下,互相学习点击去我的csdn vue中axios的封装 在vue项目和后端交互获取数据时,通常使用axios库,官方文档:https://www.npmjs ...

  3. vue中Axios的封装和API接口的管理

    前端小白的声明: 这篇文章为转载:主要是为了方便自己查阅学习.如果对原博主造成侵犯,我会立即删除. 转载地址:点击查看 如图,面对一团糟代码的你~~~真的想说,What F~U~C~K!!! 回归正题 ...

  4. vue中axios的封装以及简单使用

    一.axios的封装 在vue中为了使用axios使用方便,不需要每一个模块进行导入,就需要对其进行封装: 1.新建http.js模块 import axios from 'axios' // 设置基 ...

  5. vue中axios使用封装

    一.在main.js导入 // 引入axios,并加到原型链中 import axios from 'axios'; Vue.prototype.$axios = axios; import QS f ...

  6. vue中axios复用封装

    ajax2: function() { let that = this; return that .$http({ method: "get", url: "/Home/ ...

  7. vue中axios的封装(注意这里面异步的概念和用法十分重要)

    todo https://www.cnblogs.com/chaoyuehedy/p/9931146.html

  8. vue中Axios请求豆瓣API数据并展示到Swipe中

    vue中Axios请求豆瓣API数据并展示到Swipe中 1.首先是安装Axios: 安装方法cnpm install axios --save 等待npm安装完毕: 2.在main.js中引入axi ...

  9. vue中axios的深入使用

    如上所示一条简单的请求数据,用到了vue中axios,promise,qs等等 这里我将vue中用到的axios进行了封装方便日后使用  先对工具类进行封装utils/axios.js: // 引入模 ...

随机推荐

  1. [No0000179]改善C#程序的建议2:C#中dynamic的正确用法

    dynamic是FrameWork4.0的新特性.dynamic的出现让C#具有了弱语言类型的特性.编译器在编译的时候不再对类型进行检查,编译期默认dynamic对象支持你想要的任何特性.比如,即使你 ...

  2. [No0000131]WCF压缩传输方案整理

    1.WCF进阶:将编码后的字节流压缩传输 2.通过WCF扩展实现消息压缩 3.WCF 消息压缩性能问题及解决方法

  3. tensoflow模型中提示:ValueError: Variable rnn/basic_rnn_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? 解决办法

    在利用tensorflow框架进行模型训练的时候,有时我们需要多次训练对结果求均值来得到一个均衡的评测结论.比如训练十次求平均值.但是tf的本质就是图,当变量第一次定义使用后,第二次再使用就是提示: ...

  4. Oracle安装部署之Win7下oracle11g数据库的安装及配置

    1.下载安装包 到oracle官网downloads下下载(第一次下载需要注册账号). Win7 64位下载64位oracle11g安装包 Win7 32位下载32位oracle11g(Oracle_ ...

  5. prometheus: celery, redis-export

    https://github.com/nlighten/tomcat_exporter https://github.com/prometheus/jmx_exporter https://vexxh ...

  6. eclipse debug模式

    eclipse debug模式 1.怎样在Eclipse中设置断点 方法/步骤 1 首先打开工程项目 2 第一种是,把鼠标移动想要设置断点的行,在行号前面空白地方双击,就会出现断点 3 第二种是,在菜 ...

  7. LeetCode 762 Prime Number of Set Bits in Binary Representation 解题报告

    题目要求 Given two integers L and R, find the count of numbers in the range [L, R] (inclusive) having a ...

  8. MonkeyRunner_模拟机_运行脚本

    1.打开创建好的Android模拟机  (使用AVD Manager.exe打开,或者使用cmd窗口 emulator -avd test2打开) 2.打开cmd窗口,输入monkeyrunner,然 ...

  9. oracle闪回的使用

    1.闪回查询(原理:依赖于UNDO表空间)查询当前SCN号select current_scn from v$database;误删数据以后select * from table_name as of ...

  10. ActiveMQ整合spring、同步索引库

    1.   Activemq整合spring 1.1. 使用方法 第一步:引用相关的jar包. <dependency> <groupId>org.springframework ...