【问题标题】:How can I access the current fullPath inside of a axios plugin in Nuxt.js?如何在 Nuxt.js 的 axios 插件中访问当前的 fullPath?
【发布时间】:2020-02-08 07:01:42
【问题描述】:

我正在尝试从 Axios 的 Nuxt 插件中获取通过 route.fullPath 的当前路径。它在一定程度上起作用,但看起来因为它来自上下文,它只是在最初加载页面时设置路径。如何在 Axios 错误显示时获得当前的route.fullPath(在任何路由更改后)?

export default function ({ $axios, store, route, redirect }) {
  $axios.onError((error) => {
    if (error.response.status === 401) {
      store.commit('misc/setRedirect', route.fullPath);

      redirect('/sign-in');
    }
  });
}

【问题讨论】:

    标签: vue.js vuejs2 axios nuxt.js


    【解决方案1】:

    这似乎是一个错误,但作为一种解决方法,您可以通过插件的app 参数的app.router.currentRoute 获取当前路由:

    // plugins/axios.js
    export default function ({ $axios, app }) {
      $axios.onError(error => {
        console.log(error, app.router.currentRoute)
      })
    }
    

    【讨论】:

    猜你喜欢
    • 2021-07-24
    • 2019-08-25
    • 1970-01-01
    • 1970-01-01
    • 2020-08-25
    • 2023-04-02
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多