找回密码
 立即注册
查看: 1|回复: 1

13,拦截器request

[复制链接]

260

主题

6

精华

264

金币

技术维护QQ:515138

积分
569
发表于 7 小时前 | 显示全部楼层 |阅读模式
6.jpg
1.jpg
4.jpg 3.jpg 2.jpg


上一篇:12,发送请求获取数据案例
下一篇:14,Element Plus组件库
网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

260

主题

6

精华

264

金币

技术维护QQ:515138

积分
569
 楼主| 发表于 7 小时前 | 显示全部楼层
util/request.js
  1. //定制请求的实例
  2. //导入axios  npm install axios
  3. import axios from 'axios';
  4. //定义一个变量,记录公共的前缀  ,  baseURL
  5. const baseURL = 'http://localhost:8080';
  6. const instance = axios.create({baseURL})
  7. //添加响应拦截器
  8. instance.interceptors.response.use(
  9.     result=>{
  10.         return result.data;
  11.     },
  12.     err=>{
  13.         alert('服务异常');
  14.         return Promise.reject(err);//异步的状态转化成失败的状态
  15.     }
  16. )
  17. export default instance;
复制代码
api/article.js
  1. /* //导入axios  npm install axios
  2. import axios from 'axios';
  3. //定义一个变量,记录公共的前缀  ,  baseURL
  4. const baseURL = 'http://localhost:8080';
  5. const instance = axios.create({baseURL}) */
  6. import request from '@/util/request.j s'
  7. export function articleGetAllService() {
  8.     return request.get('/article/getAll');
  9. }
  10. export function articleSearchService(conditions) {
  11.     return request.get('/article/search', { params: conditions });
  12. }
复制代码
article.vue
  1. <scrip t setup>
  2.     import {articleGetAllService,articleSearchService} from '@/api/article.j s';
  3.     import {ref} from 'vue';
  4.    
  5.     //定义响应式数据  ref
  6.     const articleList = ref([]);
  7.     //获取所有文章数据
  8.     //同步获取articleGetAllService的返回结果  async await
  9.     const getAllArticle=async function(){
  10.         let data = await articleGetAllService();
  11.         articleList.value=data;
  12.     }
  13.     getAllArticle();
  14.    
  15.    
  16.     //定义响应式数据 searchConditions
  17.     const searchConditions = ref({
  18.         category:'',
  19.         state:''
  20.     })
  21.     //声明search函数
  22.     const search = async function(){
  23.         //文章搜索
  24.         let data = await articleSearchService({...searchConditions.value});
  25.         articleList.value = data;
  26.     }
  27. </scrip t>
  28. <template>
  29.     <!-- html元素 -->
  30.     <div>
  31.         文章分类: <input type="text" v-model="searchConditions.category">
  32.         发布状态: <input type="text" v-model="searchConditions.state">
  33.         <button v-on:click="search">搜索</button>
  34.         <br />
  35.         <br />
  36.         <table border="1 solid" colspa="0" cellspacing="0">
  37.             <tr>
  38.                 <th>文章标题</th>
  39.                 <th>分类</th>
  40.                 <th>发表时间</th>
  41.                 <th>状态</th>
  42.                 <th>操作</th>
  43.             </tr>
  44.             <tr v-for="(article,index) in articleList">
  45.                 <td>{{article.title}}</td>
  46.                 <td>{{article.category}}</td>
  47.                 <td>{{article.time}}</td>
  48.                 <td>{{article.state}}</td>
  49.                 <td>
  50.                     <button>编辑</button>
  51.                     <button>删除</button>
  52.                 </td>
  53.             </tr>
  54.         </table>
  55.     </div>
  56. </template>
复制代码


网站建设,公众号小程序开发,系统定制,软件App开发,技术维护【联系我们】手机/微信:17817817816 QQ:515138

QQ|Archiver|自丢网 ( 粤ICP备2024252464号-1 )

GMT+8, 2026-3-5 18:00

专注于网站建设,公众号小程序制作,商城小程序,系统定制,软件App开发

【联系我们】手机/微信:17817817816 QQ:515138

快速回复 返回顶部 返回列表