Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use snotify in js files #63

Open
njordr opened this issue Oct 20, 2018 · 3 comments
Open

Use snotify in js files #63

njordr opened this issue Oct 20, 2018 · 3 comments

Comments

@njordr
Copy link

njordr commented Oct 20, 2018

Hey

I'm using vue-element-admin in my project. It has an util dir with request.js file in it to make http request easier:

import axios from 'axios'
import store from '@/store'
import { getToken } from '@/utils/auth'

// create an axios instance
const service = axios.create({
  baseURL: process.env.BASE_API,
  timeout: 10000 // request timeout
})

// request interceptor
service.interceptors.request.use(config => {
  // Do something before request is sent
  if (store.getters.token) {
    config.headers['SPYREST-TOKEN'] = getToken()
  }
  return config
}, error => {
  // Do something with request error
  this.$log.error('Request error', error.response.status, error)
  Promise.reject(error)
})

// respone interceptor
service.interceptors.response.use(
  response => response,
  error => {
    this.$log.error('Request error', error.response.status, error)
    if (error.response.status === 401) {
      store.dispatch('FedLogOut').then(() => {
        location.reload()
      })
    } else {
      return Promise.reject(error)
    }
  })

export default service

There is a file (main.js) at the source root that import snotify and it works for the rest of the app.

But I want to notify the user if the REST endpoint is down, this means I have to use snotify in request.js

I tried to use snotify importing Vue object:

import Vue from 'vue'

Vue.Snotify.warn(...)

but $snotify is undefined

I tried to create a js file (utils/notifier.js):

import Snotify from 'vue-snotify'
import Vue from 'vue'

Vue.use(Snotify, {
  toast: {
    timeout: 4000,
    showProgressBar: true,
    closeOnClick: true,
    pauseOnHover: true
  }
})

const snotify = Vue.$snotify

export default snotify

importing it in request.js, but with the same result

Could you please tell me how to do?

@b1ack-ange1
Copy link

You can use Vue eventBus.
Just create vue component errorHandler and in his created() method subscribe to global event. For Example "HANDLE_ERROR". And in your interceptor error handler function you emit event: Vue.$emit("HANDLE_ERROR", error)

In vue component you can use snotify as you want.

@nros
Copy link

nros commented Nov 5, 2018

For the impatient, I see two more options:

@njordr
Copy link
Author

njordr commented Nov 5, 2018

Hey, sorry for the late reply.

@nros using it directly works like a charm, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants