【问题标题】:Set timeout error when using routes with VueJS在 VueJS 中使用路由时设置超时错误
【发布时间】:2017-09-26 21:04:42
【问题描述】:

我正在使用 VueJS,我想将数据推送到服务器,然后更改路由。

我试过这个:

saveSupportArea: function () {
  this.toast("success");
  var that = this;
  setTimeout(function(that){
    that.$router.push('/areas/');
  }, 3000);
});

但我收到此错误:

未捕获的类型错误:无法读取未定义的属性“$router”

有人可以帮忙吗?

【问题讨论】:

  • 不要将that作为匿名函数的参数传递给setTimeout
  • 天哪,它有效!你能解释为什么错了吗?和maaanyy谢谢! :) :D

标签: javascript vue.js vue-router


【解决方案1】:

不要将that作为匿名函数的参数传递给setTimeout

这样做会在匿名函数的范围内有效地重置that,因为您再次将其定义为函数的参数。该函数从未给定参数,因此它是undefined,这意味着当它尝试访问$router 属性时,thatundefined

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-13
    • 2020-01-14
    相关资源
    最近更新 更多