Home:ALL Converter>Use sweetalert2 in axios module errorHandler

Use sweetalert2 in axios module errorHandler

Ask Time:2017-12-15T17:35:28         Author:Schwesi

Json Formatter

I am creating my first nuxtjs project and am using the axios module as my HTTP client.

The Problem
Use the sweetalert2 plugin within the axios module's errorHandler.


nuxt.config.js

module.exports = {
  modules: [
    '@nuxtjs/axios'
  ],
  axios: {
    errorHandler (error) {
      if (process.browser) {
        const swal = require('sweetalert2')
        swal({
          title: 'Error', 
          text: 'Error', 
          confirmButtonColor: "#895CF2",
          confirmButtonText: 'Zurück',
          type: 'error'
        }).then(function () {
          window.history.back();
        })
    }
  },
  plugins: [
    { src: '~/plugins/sweetalert2', ssr: false }
  ],
  build: {
    vendor: ['axios', 'sweetalert2']
  }
}

plugins/sweetalert2.js

import Vue from 'vue'
import swal from 'sweetalert2'

Vue.use(swal)


The errorHandler works without sweetalert and the sweetalert code works, as is in the frontend (pages/components).

I would be very thankful for any kind of help!

Author:Schwesi,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/47829418/use-sweetalert2-in-axios-module-errorhandler
yy