【问题标题】:Change route after status 200状态 200 后更改路线
【发布时间】:2020-11-27 00:27:18
【问题描述】:

在我获得登录状态后,我试图将我的路由更改为 /despesas(状态:200)。

async EnvioLogin() {
  const response = await axios.post("api/auth/login", {
    email: this.email,
    password: this.password,
  });
  localStorage.setItem("token", response.data.token);

  const status = JSON.parse(response.data.response.status);
  if (status == "200") {
    this.$router.push("/despesas");
  } else {
    showError(true), setTimeout(showError(false), 3000);
  }
},

当我登录并在浏览器中检查时,会出现以下消息:

{data: {…}, status: 200, statusText: "OK", headers: {…}, config: {…}, …}

[Vue warn]: Error in v-on handler (Promise/async): "TypeError: Cannot read property 'status' of undefined"

【问题讨论】:

  • 看起来您在response.data.response.status 中的response 太多了。
  • 应该只是 response.status 吗?

标签: javascript vue.js async-await axios


【解决方案1】:

应该是response.status 或者你可以检查一下...

if (response.ok) {
  /* do something */
} else {
  /* do something */
}

response.ok 与您在statusText: ok 中的类似:

{data: {...}, status: 200, statusText: "ok", .... }

【讨论】:

    【解决方案2】:

    看起来response.data.response.status 未定义。

    在调用它的行上方放置一个console.log(response) 并检查结构是否正确。

    很可能是response.status

    【讨论】:

    • 我用 console.log(response) 做了 JSON.parse(response.status)(worked)。出现一个新错误是:未捕获(承诺) NavigationDuplicated:避免冗余导航到当前位置:“/despesas”。
    • 这可能是因为您是从 /despesas route 发出该请求
    • 试试 const path = /despesas if (this.$route.path !== path) this.$router.push(path)
    • 请注意,路线提示是一种解决方法。您可能应该修复您的路线或重定向。
    猜你喜欢
    • 2020-04-26
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 2016-10-02
    相关资源
    最近更新 更多