(axios拦截器)跳转页面时,加载框的显示与消失

在一个文件中创建axios路由拦截

import	 axios from 'axiso'
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    return Promise.reject(error);
  });

然后从vant中导入需要的效果显示


import { showLoadingToast ,closeToast} from 'vant';

import axios from 'axios'
axios.interceptors.request.use(function (config) {
    // Do something before request is sent
    
showLoadingToast({
    message: '加载中...',
    forbidClick: true,
  });
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
    // Any status code that lie within the range of 2xx cause this function to trigger
    // Do something with response data
    closeToast()
    return response;
  }, function (error) {
    // Any status codes that falls outside the range of 2xx cause this function to trigger
    // Do something with response error
    closeToast()

    return Promise.reject(error);
  });
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Node.js是一个基于Chrome V8引擎的JavaScript运行环境,可用于开发服务端应用程序。Axios是一个基于Promise的HTTP客户端,用于发送HTTP请求。Axios支持浏览器和Node.js环境。 在Node.js中使用Axios发送HTTP请求,可以通过以下步骤实现: 1. 安装Axios 可以通过npm命令安装Axios: ``` npm install axios ``` 2. 发送HTTP请求 可以使用Axios的get、post、put、delete等方法发送HTTP请求。例如: ``` const axios = require('axios'); axios.get('http://localhost:3000/api/user') .then(response => { console.log(response.data); }) .catch(error => { console.log(error); }); ``` 3. 添加请求拦截器 可以通过Axios的interceptors属性添加请求拦截器拦截器可以在发送请求前对请求进行处理。例如,可以在请求头中添加token等信息: ``` axios.interceptors.request.use( config => { const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }, error => { return Promise.reject(error); } ); ``` 4. 添加响应拦截器 可以通过Axios的interceptors属性添加响应拦截器拦截器可以在接收到响应后对响应进行处理。例如,可以在响应中检查是否需要跳转到登录页面: ``` axios.interceptors.response.use( response => { return response; }, error => { if (error.response.status === 401) { // 跳转到登录页面 window.location.href = '/login'; } return Promise.reject(error); } ); ``` 以上是使用Axios发送HTTP请求及添加拦截器的基本步骤。在实际应用中,还需要考虑请求的参数、响应的处理等方面。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值