【问题标题】:Where to add axios interceptors code in vue jsvue js在哪里添加axios拦截器代码
【发布时间】:2020-09-16 05:00:03
【问题描述】:

我使用 vue js 作为 laravel 的前端。我正在使用 laravel 护照进行身份验证,现在我想在收到 401 unauthentic 错误消息后显示一些错误消息,这主要发生在我的令牌过期时。所以要做到这一点,我正在使用 axios 拦截器,我的代码就像

axios.interceptors.response.use(function (response) { return response }, function (error) { // const { config, response: { status } } = error const { config, response } = error const originalRequest = config if (response && response.status === 401) { //notication or redirection this.$vs.notify({ title: 'Error', text: response.data['message'], iconPack: 'feather', icon: 'icon-check-circle', color: 'danger' }) } return Promise.reject(error) })

现在的问题是 我将此代码放在 vue js 中的位置,以便它在每次请求后调用,因此显示错误消息并重定向到登录一旦获得 401 未经授权。.

任何人的任何建议。

提前致谢!!

【问题讨论】:

    标签: laravel vue.js


    【解决方案1】:

    app.js 或将它们添加到单独的文件并包含在 app.js 中。有关于此的帖子https://medium.com/@yaob/how-to-globally-use-axios-instance-and-interceptors-e28f351bb794

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-03-27
      • 2018-04-25
      • 2018-06-05
      • 2021-10-11
      • 2023-01-21
      • 2020-07-20
      • 2020-03-04
      • 1970-01-01
      相关资源
      最近更新 更多