vue-cli配置axios,并基于axios进行后台请求函数封装
文章https://www.cnblogs.com/XHappyness/p/7677153.html已经对axios配置进行了说明,后台请求时可直接this.$axios直接进行。这里的缺点是后端请求会散乱在各个组件中,导致复用和维护艰难。
升级:将请求封装在一个文件中并加上类型声明
步骤:
1. npm install axios --save
2. src/common下建server.ts 内容如下
/**
* 后台请求设置
*/
import axios from 'axios'
// import {Notification} from 'element-ui'
import { serverUrl } from './configByEnv.js' axios.defaults.withCredentials = true;
axios.defaults.baseURL = serverUrl; axios.interceptors.request.use(function (config) {
return config;
}, function (error) {
return Promise.reject(error)
}) axios.interceptors.response.use(function (response) {
return response.data;
}, function (error) {
if (error.response.status === 401) {
alert(401);
// Notification({
// title: '权限无效',
// message: '您的用户信息已经失效, 请重新登录',
// type: 'warning',
// offset: 48
// });
// window.location.href = '/#/login'
} else {
alert('请求错误');
// Notification({
// title: '请求错误',
// message: `${error.response.status} \n ${error.config.url}`,
// type: 'error',
// offset: 48,
// })
}
return Promise.reject(error)
}) /**
* 后台请求函数
*/
class Server implements Server.IServer {
// 所有请求函数写在这里
login_async(curSlnId: Server.loginUser): Promise<string[]> {
return axios.get(`/login/${curSlnId}`).then((res: any) => res)
}
} export default new Server()
3. src/types下建server.d.ts,加入server的类型声明
declare namespace Server {
// 本文件自己用的不用加export
export interface loginUser {
name: string,
psd: string
}
export interface IServer {
login_async(loginUser: loginUser): Promise<string[]>;
}
}
4. Vue原型添加$server
(1) main.ts中添加
import server from './common/server';
Vue.prototype.$server = server;
(2)src/types下建vue.d.ts,也就是声明 Vue 插件补充的类型(https://minikiller.github.io/v2/guide/typescript.html#%E5%A3%B0%E6%98%8E-Vue-%E6%8F%92%E4%BB%B6%E8%A1%A5%E5%85%85%E7%9A%84%E7%B1%BB%E5%9E%8B),内容如下:
/* 补充Vue类型声明 */
import Vue from 'vue' // 注意要用这一步
declare module 'vue/types/vue' {
interface Vue {
$server: Server.IServer;
}
}
5. 可以在任何组件中用this.$server使用封装的请求方法了,还有有类型检查和类型提示。
vue-cli配置axios,并基于axios进行后台请求函数封装的更多相关文章
- vue-ajax/axios请求函数封装: axios+promise
项目文件目录/src/api ajax.js /** * ajax 请求函数模块 * 返回值为promise对象 */ import axios from 'axios' export default ...
- 010——VUE中使用lodash库减少watch对后台请求的压力
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 【VUE】vue在vue-cli3环境下基于axios解决跨域问题
网上的绝大部分教程解决vue+axios跨域问题都不能直接适用vue-cli3.这是因为vue-cli3不一样的配置方式导致的. 如果是使用vue-cli3构建的项目,那么默认是没有config.js ...
- vue cli 配置信息说明
摘自csdn http://blog.csdn.net/hongchh/article/details/55113751
- Vue基础知识之vue-resource和axios(三)
vue-resource Vue.js是数据驱动的,这使得我们并不需要直接操作DOM,如果我们不需要使用jQuery的DOM选择器,就没有必要引入jQuery.vue-resource是Vue.js的 ...
- Vue基础知识之vue-resource和axios
Vue基础知识之vue-resource和axios 原文链接:http://www.cnblogs.com/Juphy/p/7073027.html vue-resource Vue.js是数据驱 ...
- [Vue 牛刀小试]:第十七章 - 优化 Vue CLI 3 构建的前端项目模板(1)- 基础项目模板介绍
一.前言 在上一章中,我们开始通过 Vue CLI 去搭建属于自己的前端 Vue 项目模板,就像我们 .NET 程序员在使用 asp.net core 时一样,我们更多的会在框架基础上按照自己的开发习 ...
- Vue 使用lodash库减少watch对后台请求压力
lodash需要新引入 我使用的是npm方式 使用lodash的_.debounce方法 具体代码: <!doctype html> <html lang="en" ...
- vue cli+axios踩坑记录+拦截器使用,代理跨域proxy(更新)
16319 1.首先axios不支持vue.use()方式声明使用,看了所有近乎相同的axios文档都没有提到这一点建议方式 在main.js中如下声明使用 import axios from 'ax ...
随机推荐
- git如何到精通
git教程 目录 一.版本控制概要 1.1.什么是版本控制 1.2.常用术语 1.3.常见的版本控制器 1.4.版本控制分类 1.4.1.本地版本控制 1.4.2.集中版本控制 1.4.3.分布式 ...
- windows server dump文件
1. mini dump: ***** 需要包含 dbghelp.dll 库 ****mini_dump.h文件: // reference:https://msdn.microsoft.com/zh ...
- EAC 抓取CD为AAC文件
下载EAC v1.3 下载FAAC 安装完EAC以后进入主界面,选菜单EAC->compression option,如图: 选User Defined Encoder,选择之前解压好的faac ...
- Hystrix使用
Hystrix是Netflix开源的一款容错系统,能帮助使用者码出具备强大的容错能力和鲁棒性的程序.如果某程序或class要使用Hystrix,只需简单继承HystrixCommand/Hystrix ...
- 20155310马英林 实验2 Windows口令破解
实 验 报 告 实验名称: 实验二 口令破解 姓名:马英林 学号: 20155310 班级: 1553 日期: 2017.10.24 一. 实验环境 •系统环境:Windows •网络环境:交换网络结 ...
- 20155317 王新玮 2006-2007-2 《Java程序设计》第4周学习总结
20155317 王新玮 2006-2007-2 <Java程序设计>第4周学习总结 教材学习内容总结 第六章 继承共同行为 多个类中存在相同属性和行为时,将这些内容抽取到单独一个类中,那 ...
- c++静态变量
静态变量 一.静态变量 static关键字 static int i; 二.静态变量的运用 .计算函数被调用次数 .返回指针 第45课中有这么一段 int* square3(int *x) { int ...
- P3940 分组
P3940 分组 https://www.luogu.org/problemnew/show/P3940 官方题解http://pan.baidu.com/s/1eSAMuXk 分析: 并查集. 首先 ...
- 求助:springboot调用存储过程并使用了pagehelper分页时报错com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException
存储过程如下: dao层的sql Controller层调用: html页面 没有使用pagehelper分页之前,可以正常使用 使用了pagehelper之后就报错 ### Error queryi ...
- Drupal7 针对特定条件才显示区块
当D7中开启PHP filter模块. Text format就会多出“PHP Code”选项. 而且,新建block时也会多出"Pages on which PHP code return ...