首页前端开发JavaScriptvue中axios封装使用的完整教程

vue中axios封装使用的完整教程

时间2024-02-01 00:50:03发布访客分类JavaScript浏览355
导读:收集整理的这篇文章主要介绍了vue中axios封装使用的完整教程,觉得挺不错的,现在分享给大家,也给大家做个参考。 前言如今,在项目中,普遍采用Axios库进行Http接口请求。它是基于...
收集整理的这篇文章主要介绍了vue中axios封装使用的完整教程,觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

如今,在项目中,普遍采用Axios库进行Http接口请求。它是基于promise的http库,可运行在浏览器端和node.js中。此外还有拦截请求和响应、转换JSON数据、客户端防御XSRF等优秀的特性。

考虑到各个项目实际使用时写法混乱,不统一。对Axios进行一下通用化的封装,目的是帮助简化代码和利于后期的更新维护,尽量通用化。

方法如下

1. vue安装axios

	npm install axios -s	或者	npm i axios -S

2. 在main.js进行全局引入

	import axios From 'axios'	Vue.PRototyPE.$axios = axios //将axios绑定到vue的原型上

3. 配置跨域 在根目录下vue.config.js里边

	module.exports = {
	 publicPath: './',	 //配置跨域请求	 devServer: {
	  open: true, //是否自动打开浏览器	  https: false, //是否开启https	  hotOnly: false,	  Proxy: {
 // 配置跨域	   '/api': {
	    target: 'http://********', //请求接口域名 	    ws: true,	    secure: false,	    changOrigin: true, //是否允许跨越	    pathrewrITe: {
	     '^/api': ''	    }
	   }
	  }
    ,	  before: app =>
 {
 }
	 }
	}
    

4. 在src子目录下的api文件夹下创建api.js文件进行简单的封装axios

import axios from 'axios'//这里引用了element的loading全屏加载import {
 Loading }
     from "element-ui";
const service = axios.create({
 baseURL: '/', timeout: 30000 // 设置请求超时时间}
    )let loading = "";
    // 请求拦截器service.interceptors.request.use( (config) =>
 {
  // 在请求发送之前做一些处理  if (!(config.headers['Content-type'])) {
   loading = Loading.service({
    lock: true,    text: "加载中...",    spinner: "el-icon-loading",    background: "rgba(255,255,255,0.7)",    customClass: "request-loading",   }
    );
   if (config.method == 'post') {
        config.headers['Content-Type'] =     'application/json;
charset=UTF-8'    for (VAR key in config.data) {
     if (config.data[key] === '') {
      delete config.data[key]     }
    }
    config.data = JSON.stringify(config.data)   }
 else {
        config.headers['Content-Type'] =     'application/x-www-form-urlencoded;
charset=UTF-8'    config.data = JSON.stringify(config.data)   }
  }
  const token = "token"  // 让每个请求携带token-- ['X-Token']为自定义key 请根据实际情况自行修改  if (token) {
   config.headers['Authorization'] = token  }
  return config }
    , (error) =>
 {
      loading.close();
  // 发送失败  console.LOG(error)  return Promise.reject(error) }
    )// 响应拦截器service.interceptors.response.use( (response) =>
 {
      loading.close();
      // dataAxios 是 axios 返回数据中的 data  // loadingInstance.close();
  const dataAxios = response.data  // 这个状态码是和后端约定的  return dataAxios }
    , (error) =>
 {
  return Promise.reject(error) }
    )	export default service

5. 在api文件夹下创建http文件

 // 引入封装好的axios // ps:如果没有封装,正常引入axios即可  import axios from "./api";
    	// 	/api为配置跨域的路径变量  let reportUpload= '/api/report/upload'  export const Upload= () =>
 {
   return axios.get( reportUpload )  }
    

6. 在页面中调用接口

// 引入封装好的接口 	import {
 Upload}
     from "@/api/http.js";
 // 调用时使用 async Upload() {
  let {
 result }
     = await getlist ();
  	console.log(result) }
    ,

总结

到此这篇关于vue中axios封装使用的文章就介绍到这了,更多相关vue axios封装使用内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

您可能感兴趣的文章:
  • 使用Vue-axios进行数据交互的方法
  • vue项目实战之优雅使用axios
  • 如何在Vue项目中使用axios请求
  • vue全局使用axios的操作
  • vue中axios的使用详解

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!

封装axios及使用

若转载请注明出处: vue中axios封装使用的完整教程
本文地址: https://pptw.com/jishu/594702.html
在c语言中,引用数组元素时,其数组下标的数据类型允许是什么? c语言文件读写操作有哪些?

游客 回复需填写必要信息