Nuxt.js封装@axios-nuxt

1. 注意安装插件 yarn add @nuxtjs/axios yarn add @nuxtjs/proxy

2.在plugins文件夹新建axios.js 内容如下:

import { Message ,Loading} from 'element-ui'
export default function ({ store, redirect, app: { $axios } }) {
  let loadingInstance = null;
  // 后端接口地址
  // $axios.defaults.baseURL = 'https://aabbcc.com/api'
  $axios.defaults.baseURL = process.env.NODE_ENV == "development" ? "/api" : "https://aabbcc.com/api"

  // Request拦截器:设置Token
  $axios.onRequest((config) => {
    //  使用Vuex存储Token,并做持久化处理
    // console.log(localStorage.getItem("token"))
    let noLoda = config.noLoad;
    if (!noLoda) {
      loadingInstance = Loading.service({
        fullscreen: true,
        background: "#fff",
      });
    }
    config.headers['Authorization'] = "Bearer" + localStorage.getItem("token")
  })
  // Error拦截器:出现错误的时候被调用,根据状态码做对应判断并显示全局Message
  $axios.onError((error) => {
    if (loadingInstance) {
      loadingInstance.close();
      loadingInstance = null;
    }
    const code = parseInt(error.response && error.response.status)
    switch (code) {
      case 400:
        Message.error('请求错误')
        break;
      // 未登录
      case 401:
        Message.error("Token过期")
        break;
      case 403:
        Message.error('拒绝访问')
        break;
      case 404:
        Message.error(`请求地址出错: ${error.response.config.url}`)
        break;
      case 408:
        Message.error('请求超时')
        break;
      case 500:
        Message.error('服务器内部错误')
        break;
      case 501:
        Message.error('服务未实现')
        break;
      case 502:
        Message.error('网关错误')
        break;
      case 503:
        Message.error('服务不可用')
        break;
      case 504:
        Message.error('网关超时')
        break;
      case 505:
        Message.error('HTTP版本不受支持')
        break;
      default:
        break;
    }
    // 异常信息
    // Message.error(error.response.data.message)
  })
  // Response拦截器:对正常返回的数据进行处理
  $axios.onResponse((response) => {
    if (loadingInstance) {
      loadingInstance.close();
      loadingInstance = null;
    }
    return response.data
  })
}

3.找到nuxt.config.js配置文件添加以下内容:

  plugins: [
    { src: '~/plugins/axios.js' ,ssr: false },
  ],
  axios: {
    proxy: true,
    prefix: '/api', // baseURL
    credentials: true,
  },
  proxy: {
    '/api': {
      target: 'https://aabbcc.com/api', // 代理地址
      changeOrigin: true,
      pathRewrite: {
        '^/api': '', //将 /api 替换掉
      }

    }
  },
  modules: [
    '@nuxtjs/axios',
    "@nuxtjs/proxy"
  ],

4.请求使用方法:

this.$axios.post('/duanxin', {
        phone: this.iphoneLoginForm.iphone,
      }).then((res) => {
        this.$message.success(res.message);
      })
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端宇宙

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值