VUE axios请求 封装 get post Http
创建httpService.js 文件
import axios from 'axios';
import { Loading , Message } from 'element-ui';
import { Vuevm } from '../main';
window.Vuevm = Vuevm;
const BASE_URL = process.env.BASE_API; //域名路径 const formatParams = (data)=>{
let arr = [];
for (let name in data) {
arr.push(encodeURIComponent(name) + "=" + encodeURIComponent(data[name]));
}
return arr.join("&");
} axios.defaults.timeout = 15000; //超时时间 export const httpService = (url,params,method,loading=true) => {
if(loading)
{
var loadingInstance = Loading.service({
lock: true,
text: '加载中',
spinner: "el-icon-loading",
background: "rgba(0, 0, 0, 0.7)"
});
} if(method === 'post')
{
return new Promise((resolve, reject) => {
axios.post(BASE_URL+url, params,{
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(data=>{
if(loading) loadingInstance.close();
resolve(data.data); }).catch(error=>{
reject(error);
Message.error('网络不给力');
if(loading) loadingInstance.close();
});
})
}
else
{
return new Promise((resolve, reject) => {
axios.get(BASE_URL+url+'?'+formatParams(params)).then(data=>{
resolve(data.data);
if(loading) loadingInstance.close();
}).catch(error=>{
Message.error('网络不给力');
if(loading) loadingInstance.close();
reject(error);
});
})
}
}
创建 使用的接口API 文件 api.js
import { httpService } from './httpService';
/**
* @name 医院列表
* @param {*} params
*/
export const hospitalList = (params) =>{
return httpService('hospital/list',params,'get');
}
/**
* @name 医院详情
* @param {*} params
*/
export const hospitalDetail = (params) =>{
return httpService('/hospital/detail',params,'get');
}
页面引用 使用 index.vue
<template>
<div class="commodit"> </div>
</template> <script>
import { hospitalList, hospitalDetail } from "@/api/hospital";
export default {
name: "Commodit",
components: { },
data() {
return {
tableData: [],
};
},
created() {
this.hospitalList()
},
methods: {
//获取 医院列表
async hospitalList() {
var data = { hospitalName: '', pageSize: 10, pageNum: 1 };
const listData = await hospitalList(data);
console.log(listData)
if( listData.code==0 ){
this.tableData = listData.data.records
} },
}
};
</script> <style scoped lang="scss">
@import "../../filters/css/all.css"; .commodit { }
</style>
VUE axios请求 封装 get post Http的更多相关文章
- Vue: axios 请求封装及设置默认域名前缀 (for Vue 2.0)
1. 实现效果 以get方法向http://192.168.32.12:8080/users 发起请求.获取数据并进行处理 this.apiGet('/users', {}) .then((res) ...
- axios请求封装和异常统一处理
前端网络请求封装前端采用了axios来处理网络请求,为了避免在每次请求时都去判断各种各样的网络情况,比如连接超时.服务器内部错误.权限不足等等不一而足,我对axios进行了简单的封装,这里主要使用了a ...
- vue axios接口封装、Promise封装、简单的axios方法封装、vue接口方法封装、vue post、get、patch、put方法封装
相信大家在做前后端数据交互的时候都会给请求做一些简单的封装就像之前封装ajax方法一样axios的封装也是一样的简单下面这个就是封装的axios的方法,require.js import axios ...
- 10. vue axios 请求未完成时路由跳转报错问题
axios 请求未完成时路由跳转报错问题 前两天项目基本功能算是完成了,在公司测试时遇到了遇到了一个问题,那就是在请求未完成时进行路由跳转时会报错,想了几种办法来解决,例如加loading,请求拦截, ...
- Vue Axios 的封装使用
目录 Axios 说明 安装 Axios 请求配置 响应结构 常用请求方法 默认值配置 全局的 请求配置项 自定义实例默认值 配置的优先顺序 拦截器 个人完整 axios 配置 Axios 说明 Ax ...
- vue axios 请求 https 的特殊处理
最近遇到自签发的CA证书,在前端axios请求https请求时,无法自动加载证书. 解决方法:将无法加载的请求在浏览器新窗口手动加载,选择继续连接. 重新加载,问题解决. 根本原因:因为自签发证书,浏 ...
- vue axios 请求本地接口端口不一致出现跨域设置代理
首先在config下面的index.js,设置跨域代理 在axios请求的时候 用'/api/' 替代baseURL 最重要的就是设置完必须重新 npm run dev 否则不生效
- vue+axios请求头封装
import { mapMutations } from 'vuex' import axios from 'axios' import { Toast } from 'mint-ui'; impor ...
- vue axios 简单封装以及思考
先安装 axios npm install axios axios的详细介绍以及用法 就不多说了请 移步 github ➡️ https://github.com/axios/axios 下面是简单 ...
随机推荐
- tips [ 18870 ]
Created at 2017-08-23 Updated at 2018-01-31 Category 东方大陆 Tag 东方大陆 上面有编辑时间的,别吐槽说什么过期内容了使用 lightPIC图床 ...
- 算法基本概念及常用算法Python实现
基础算法概念: 时间复杂度 时间复杂度是从其增速的角度度量的, 时间复杂度一般用大O法表示. 递归 递归指的是调用自己的函数. 如果使用循环,程序性能可能更高: 如果使用递归,程序可能更容易理解. ...
- 重启aliyun esc 需要重新启动redis
/export/sorftware ./redis-server ../redis.conf redis-server 配置路径 如redis-server /etc/redis.conf 不是后 ...
- AI超越人类大脑,或许是场“别有用心者”的骗局
谷歌.微软.苹果.特斯拉.百度.腾讯.阿里等互联网巨头企业,以及纳德拉.马斯克.扎克伯格.马云等互联网大佬,近年来一直都对人工智能--AI非常上心.在众多场合对AI给予了或肯定,或恐惧的评价.但无 ...
- 【问】:和=在map里面的区别
- LitePal
Litepal采用的是对象关系映射(ORM)模式 LitePal的配置工作. 1.添加依赖 compile 'org.litepal.android:core:1.3.2' 2.配置lite ...
- 曹工说Spring Boot源码(22)-- 你说我Spring Aop依赖AspectJ,我依赖它什么了
写在前面的话 相关背景及资源: 曹工说Spring Boot源码(1)-- Bean Definition到底是什么,附spring思维导图分享 曹工说Spring Boot源码(2)-- Bean ...
- 华为物联网IoT学习笔记目录
实验准备: 1.<小熊派功能介绍> 2.<小熊派硬件分析> 3.<STM32Cube软件安装介绍> 4.<MDK软件安装介绍> 5.<华为 IoT ...
- 峰哥说技术:04-Spring Boot基本配置
Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 04 Spring Boot基本配置 1)容器的相关配置 在Spring Boot中可以内置Tomcat. ...
- 小程序Echarts 构建中国地图并锚定区域点击事件
小程序Echarts 构建中国地图并锚定区域点击事件 Step1 效果展示 使用的绘图框架为 Echarts for Wexin 具体API文档地址请点击 ----> Step2 条件准备 1. ...