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 ...
随机推荐
- linux中配置JDK环境变量
使用的centos版本为 7.5 首先我们要把jdk拷到linux中,这里我们借助XShell工具,我们先来看看Xshell的用法 打开Xshell 后点击文件,“新建“,如下图: 起一个名称,主机填 ...
- SOC中的DFT和BIST对比与比较-IC学习笔记(二)
ATE:ATE是Automatic Test Equipment的缩写,根据客户的测试要求.图纸及参考方案,采用MCU.PLC.PC基于VB.VC开发平台,利用TestStand&LabVIE ...
- High Water Mark 图示
+---- high water mark of newly created table | V +-------------------------------------------------- ...
- 成都Uber优步司机奖励政策(4月14日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- day2 HTML - body
<body>内常用标签 1.基本标签 所有标签分为: # 块级标签: div(白板),H系列(加大加粗),p标签(段落和段落之间有间距) # 行内标签: span(白板) 1. 图标, ...
- Spring中的TransactionProxyFactoryBean作用及配置(转)
问: 原文链接 http://blog.csdn.net/cpp_lzth/article/details/6551703 看AOP的时候发现spring中有个org.springframework. ...
- 【转】 mysql使用federated引擎实现远程访问数据库(跨网络同时操作两个数据库中的表)
原文转自:http://www.2cto.com/database/201412/358397.html 问题: 这里假设我需要在IP1上的database1上访问IP2的database数据库内的t ...
- mysql 错误代码 1248
1248 - Every derived table must have its own alias (MYSQL错误) 这句话的意思是说每个派生出来的表都必须有一个自己的别名,给派生表加上一个别名就 ...
- python的30个编程技巧
1.原地交换两个数字 x, y =10, 20 print(x, y) y, x = x, y print(x, y) 10 20 20 10 2.链状比较操作符 n = 10 print(1 &l ...
- 5.openldap设置用户本身修改密码
1. 修改slapd.conf文件 #vim /etc/openldap/slapd.conf 修改下如下内容 access to dn.subtree="ou=People,dc=bawo ...