【问题标题】:How to use SweetAlert2 inside AXIOS.interceptors.response.use()如何在 AXIOS.interceptors.response.use() 中使用 SweetAlert2
【发布时间】:2019-01-13 16:18:15
【问题描述】:

我尝试在我的 vue 项目中加入 SweetAlert2,并且能够在我的 main.js 上使用 Vue.use() 在我的视图中全局访问 swal() 函数。

但是当我尝试在AXIOS.interceptors.response.use() 中使用它时,函数swal() 变成了undefined

这基本上就是我的interceptor.js 文件的样子。

import axios from 'axios'

let instance = axios.create({
   baseURL: '/'
});

export const AXIOS = instance;

AXIOS.interceptors.response.use(

   function(response) {
      // $swal() becomes undefined
      this.$swal('Error', 'Some kind of error', 'error')
      return response;
   }, 

   function(error) {

      // TODO:

      return Promise.reject(error)
   }
)

关于如何合并它的任何想法?或者你们对如何在全球范围内处理响应有更好的想法?

【问题讨论】:

    标签: vue.js axios


    【解决方案1】:

    当您在回调函数中使用 this 的上下文时,它可能不再属于您的 Vue 实例。

    尝试使用以下内容修改您的interceptor.js:

    import Vue from 'vue';

    而不是使用this.$swal('Error', 'Some kind of error', 'error')

    使用Vue.swal('Error', 'Some kind of error', 'error');

    【讨论】:

      猜你喜欢
      • 2021-10-28
      • 2016-12-05
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2018-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多