首页 > 解决方案 > beforeEach 中的承诺(Nuxt/vue-router)

问题描述

我试图在路由更改之前发送 POST 请求以验证用户凭据,我最终在 Nuxt 插件中实现了该逻辑。但 Nuxt 不会等待承诺得到解决。我怎样才能做到这一点?我试过使用 async/await 但结果是一样的。

export default async ({ app }, inject) => {
  app.router.beforeEach(function(to, from, next) {
    if (app.$cookies.get('JWT')) {
        app.$axios.$post('http://localhost:3000/verify', '2').then((res) => {
          console.log('We are here! This is expected to continue...')
          next()
        })
    } else {
        console.log('No cookie!')
        next(false)
    }
  })
}

截屏

*编辑:添加屏幕截图

标签: javascriptvue.jsaxiosvue-routernuxt.js

解决方案


推荐阅读