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 ...
随机推荐
- Vim Go开发环境搭建
基本搭建流程参考了网上的博文以及Vimgo的Github主页 博文https://www.cnblogs.com/breg/p/5386365.html Vim-go主页(我能不能加入项目,做点贡献呢 ...
- 20145226夏艺华 网络对抗技术 EXP7 网络欺诈技术防范
20145226夏艺华 网络对抗技术 EXP7 网络欺诈技术防范 实践内容 本实践的目标理解常用网络欺诈背后的原理,以提高防范意识,并提出具体防范方法. · 简单应用SET工具建立冒名网站 · ett ...
- 【SQLSERVER】服务挂起解决办法
一. 问题描述:某项SQLSERVER服务,运行状态为“正在挂起更改”,导致该服务无法使用,也不能启动.停止.重新启动. 二.解决方法 方法一:从任务管理器 → 进程 (勾上 显示所有用户进程) → ...
- 【转载】C/C++杂记:运行时类型识别(RTTI)与动态类型转换原理
原文:C/C++杂记:运行时类型识别(RTTI)与动态类型转换原理 运行时类型识别(RTTI)的引入有三个作用: 配合typeid操作符的实现: 实现异常处理中catch的匹配过程: 实现动态类型转换 ...
- 快读板子fread
struct ios { inline char read(){ <<|; static char buf[IN_LEN],*s,*t; ,IN_LEN,stdin)),s==t?-:*s ...
- 暗通道去雾算法的python实现
何凯明博士的去雾文章和算法实现已经漫天飞了,我今天也就不啰里啰唆,直接给出自己python实现的完整版本,全部才60多行代码,简单易懂,并有简要注释,去雾效果也很不错. 在这个python版本中,计算 ...
- 一起来做Chrome Extension《一些问题》
目录 Unchecked runtime.lastError: The message port closed before a response wa received. 使用 eval Conte ...
- Zigbee系列(end device)
End device设备分为睡眠和非睡眠两种(RxOnWhenIdle标记不同). 入网时的association请求,会使用这个标记. 共同特性 子节点多次发送数据失败(无回应),发送孤点扫描(re ...
- DeepLearning - Regularization
I have finished the first course in the DeepLearnin.ai series. The assignment is relatively easy, bu ...
- eBay推Winit海外仓 鼓励卖家拓展北美市场
[亿邦动力网讯]2月11日消息,日前,跨境电商平台eBay与外贸电商服务商万邑通(Winit)合作,针对平台卖家推出了Winit美国海外仓,鼓励卖家拓展北美市场. 亿邦动力网获悉,Winit美国海外仓 ...