创新实训博客(14)——Vue前端与后端的接口交互
2020-07-14 06:21
381 查看
以获取博文内容的数据为例子,来演示前后端数据交互:
用到的模块
主要是axios、qs
封装request
[code]import axios from 'axios' import { Message } from 'element-ui' import store from '@/store' import { getToken } from '@/utils/auth' // 设置请求的地址 const service = axios.create({ baseURL: '/api', timeout: 5000 }) // 构造请求 service.interceptors.request.use( config => { // 传递token,除了登录的时候没有token以外都要传 if (store.getters.token) { config.headers['Authorization'] = getToken() } return config }, error => { // 错误 return Promise.reject(error) } ) // 发完请求以后的响应结果 service.interceptors.response.use( response => { // 后期需要返回的数据,先放着 const res = response.data // 我的要求:200代表成功 if (res.code !== 200) { // 设置错误信息 var out_msg = (res.code + ' - ' + res.msg) || (res.code + ' - ' + '错误!') Message({ message: out_msg, showClose: true, type: 'error', duration: 5 * 1000 }) // 是否需要重新设置token if (res.code === 50000) { // 跳转到resetToken store.dispatch('user/resetToken').then(() => { location.reload() }) } return Promise.reject(new Error(out_msg)) } else { Message({ message: res.msg || '成功!', showClose: true, type: 'success', duration: 5 * 1000 }) return res } }, error => { Message({ message: error.message, showClose: true, type: 'error', duration: 5 * 1000 }) return Promise.reject(error) } ) export default service
api管理
按照模块的分类,同一种类型的api放在同一个js文件下。
不同的api类型放在不同的文件中,所有的vue文件都是import这些模块后调用。
向后端发请求
[code]// 获取最近的文章列表 export function getRecentArticles(page, size) { return request({ url: '/article/list', method: 'get', params: { page: page, size: size } }) }
js处理返回的数据
比如我需要进行html格式的处理,还有获取相似文章列表并且存储起来。
[code]created() { var that = this getArticle(that.id).then(response => { const { data } = response that.blog = data that.blog.content = '<html><head><style> img{ max-width: 100%; width:auto; height: auto; }</style></head><body>' + that.blog.content + '</body></html>' readArticle(that.id).then(response2 => { getSimilarityArticle(that.id).then(response3 => { const { data } = response3 that.similarList = data }) }) }) }
在html中显示得到的数据
对于列表项,使用v-for进行显示。
[code] <div v-for="item in similarList" :key="item.id"> <blog-brief-item :id="item.id" :title="item.title" :author="item.author" :date="item.updateTime" /> </div>
其他在前端完成的操作
以tag的分类展示为例子,在存储的时候是以#作为分隔符进行分割。
那么我传到前端以后的数据,应该使用一个split函数来进行处理。
[code] splitDescription(data) { return data.split('#') }
在vue中也需要进行相应的操作
[code] <el-table-column align="center" label="标签描述"> <template v-if="splitDescription(scope.row.description)[0]!=='\n'" slot-scope="scope"> <span v-for="item in splitDescription(scope.row.description)" :key="item"> <el-tag type="info" effect="plain" style="margin: 4px">{{ item }}</el-tag> </span> </template> </el-table-column>
最终完成的显示效果
相关文章推荐
- 创新实训博客(15)——Android开发中Kotlin与后端的接口交互
- 创新实训博客(31)——Vue.js前端项目的生产环境部署
- asp.net 前端跟后端通过接口调用实现数据交互(含分页)
- 创新实训博客(29)——接口调用请求汇总(可视化部分)
- Vue中使用axios(POST请求)调用后端接口时,后端接口接收不到前端参数的问题
- 前端与后端的交互(定义接口)
- 创新实训博客(28)——接口调用请求汇总(App和博客相关部分)
- 创新实训博客(45)——Vue打包GitHub Page以及遇到的坑
- 创新实训博客(27)——接口调用请求汇总(博客相关部分)
- 创新实训博客(26)——接口调用请求汇总(后台管理部分)
- 创新实训博客(25)——接口调用请求汇总(用户基本操作和历史记录相关部分)
- 创新实训博客(42)——搜索界面自动补全的前端处理
- $on $once $off $emit $mount 如何一次进行多个异步请求 拦截器 响应体 插槽 在Vue中如何进行前后端数据的交互 你在工作当中如何对接口进行管理的
- 创新实训博客(21)——Vue中使用ECharts进行数据可视化(进阶部分)
- 创新实训博客(20)——Vue中使用ECharts进行数据可视化(基本图表部分)
- Vue前后端接口交互—— Axios api 接口文件配置
- 前后端分离简单架构搭建 Vue-Cli 后端接口Api+Swagger(1) -前端
- 创新实训博客(34)——用户画像部分前端展示
- 创新实训博客(18)——Vue中标签页组件动态化
- 配置vue脚手架项目和后端交互过程