vue+axios+element-ui实战(二)--axios请求封装
2019-03-14 14:47
746 查看
上一篇我们搭建了vue项目的简单框架,现在来讲一下请求后端接口的封装,我们这里用的是axios,大家可以查看中文文档:https://www.kancloud.cn/yunye/axios/234845
安装:
[code]npm install axios
安装完成后,我们先在src目录下新建一个utils文件夹,然后在utils里面新建request.js文件,对照着axios中文文档进行封装:
[code]import axios from 'axios' import { Message, Loading } from 'element-ui' import router from '@/router' // create an axios instance const service = axios.create({ baseURL: process.env.BASE_API, // api的base_url timeout: 30000 // request timeout }) // request interceptor // 添加请求拦截器 service.interceptors.request.use(config => { // Do something before request is sent config.headers['Cache-Control'] = 'no-cache' // ie清缓存,否则无法正常刷新 config.headers['Pragma'] = 'no-cache' // HTTP/1.1版本,ie清缓存,否则无法正常刷新 if (config.formType && config.formType === 1) { config.headers['Content-Type'] = 'application/x-www-form-urlencoded' config.transformRequest = [function (data) { // Do whatever you want to transform the data let ret = '' for (let it in data) { ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&' } return ret.substring(0, ret.length - 1) }] } if (config.showLoading) { showFullScreenLoading() } return config }, error => { // Do something with request error Promise.reject(error) }) // respone interceptor // 添加响应拦截器 service.interceptors.response.use( response => { if (response.config.showLoading) { tryHideFullScreenLoading() } var res = response.data // 以下请根据后端返回具体格式修改!!!!! if (res.code === 200) { return res } else { Message({ message: res.message, type: 'error', duration: 5 * 1000 }) return Promise.reject(res) } }, error => { // 错误处理 tryHideFullScreenLoading() Message({ message: error.message, type: 'error', duration: 5 * 1000 }) return Promise.reject(error) } ) // 加载封装 let loading function startLoading () { loading = Loading.service({ lock: true, text: '加载中', background: 'rgba(0, 0, 0, 0.7)' }) } // 关闭加载 function endLoading () { loading.close() } let needLoadingRequestCount = 0 export function showFullScreenLoading () { if (needLoadingRequestCount === 0) { startLoading() } needLoadingRequestCount++ } export function tryHideFullScreenLoading () { if (needLoadingRequestCount <= 0) return needLoadingRequestCount-- if (needLoadingRequestCount === 0) { endLoading() } } export default service
然后,我们配置一下config文件下的index.js文件里面的proxyTable:
[code]proxyTable: { '/api': { target: 'https://api.apiopen.top/', secure: false, // 如果是https接口,需要配置这个参数 changeOrigin: true, // 如果接口跨域,需要进行这个参数配置 pathRewrite: { '^/api': '/' } } }
这样,我们可以把接口都统一管理,在src文件夹下新建文件夹api,新建index.js文件,把接口请求都放在这里:
[code]import request from '@/utils/request' export function musicBroadcastingDetails (params) { return request({ url: `musicBroadcastingDetails?channelname=${params.channelname}`, method: 'get', data: params, showLoading: true // loading效果 }) }
在具体页面上调用:
[code]<template> <div> <div style="padding: 30px 0; border-bottom: 1px solid #ccc;"> <div>请求接口示例</div> <div>element UI表格形式展示</div> <el-table :data="tableData" style="width: 100%"> <el-table-column prop="artist" label="歌手" width="180"> </el-table-column> <el-table-column prop="title" label="歌曲" width="180"> </el-table-column> <el-table-column prop="thumb" label="歌曲地址"> </el-table-column> </el-table> </div> </div> </template> <script> import { musicBroadcastingDetails } from '@/api/index.js' export default { name: "index", data() { return { tableData: [], } }, mounted() { this.requestApi() }, methods: { requestApi() { let channelname = 'public_tuijian_spring' musicBroadcastingDetails({channelname}).then( res => { let data1 = JSON.stringify(res) // 因示例调用网上开放接口返回json数据太多,需做转换 let data = JSON.parse(data1) this.tableData = data.result.songlist for (let i in data.result.songlist) { this.songDatas.push({artist: data.result.songlist[i].artist, title: data.result.songlist[i].title, thumb: data.result.songlist[i].thumb}) } }) }, } } </script> <style scoped lang="scss"> </style>
效果如下:
相关文章推荐
- vue+axios+element-ui实战(三)--post请求下载处理
- vue+axios+element-ui 实现ajax请求拦截和路由拦截
- vue+axios+element-ui实战(五)-- 验证、清除多个form表单
- vue+axios+element-ui实战(四)-- 中国省市区级联选择
- VueJs2.0入门--之后台管理系统(vue.js +vue-router+vuex+element-ui+axios)
- 基于Vue+element-ui 的Table二次封装的实现
- vue 基于element-ui 分页组件封装的实例代码
- vue中使用axios+Promise二次封装ajax请求
- vue-cli快速构建工程 ElementUI axios router 引入使用
- VUE2.0+Element-UI+Echarts封装的组件实例
- vue2.0项目实战 使用axios发送请求
- Vue项目中对axios进行封装以及api接口请求
- Spring boot+Shiro+ spring MVC+swagger UI +Mybatis+mysql+Vue +Element UI 之三 vue简单页面实战
- 【小白成长之路】实战vue2.0+axios 接口封装
- vue axios跨域、vue+elementUI+axios代理服务
- [置顶] 用Vue、element-ui、axios实现省市区三级联动
- vuecli+element-ui+sass+axios的搭建
- Vue: axios 请求封装及设置默认域名前缀 (for Vue 2.0)
- VUE2.0+Element-UI+Echarts封装的组件
- vue中axios 配置请求拦截功能 及请求方式如何封装