在 VueJS 中使用路由时设置超时错误
Set timeout error when using routes with VueJS
我正在使用 VueJS,我想将数据推送到服务器,然后再更改路由。
我试过这个:
saveSupportArea: function () {
this.toast("success");
var that = this;
setTimeout(function(that){
that.$router.push('/areas/');
}, 3000);
});
但是我收到这个错误:
Uncaught TypeError: Cannot read property '$router' of undefined
有人可以帮忙吗?
不要将 that
作为匿名函数的参数传递给 setTimeout
。
这样做有效地在匿名函数的范围内重置了 that
,因为您再次将其定义为函数的参数。该函数从未被赋予参数,因此它是 undefined
,这意味着当它尝试访问 $router
属性 时 that
是 undefined
。
您可以使用 BeforeRouteLeave 函数,然后在该函数中描述 setTimeout。
我正在使用 VueJS,我想将数据推送到服务器,然后再更改路由。
我试过这个:
saveSupportArea: function () {
this.toast("success");
var that = this;
setTimeout(function(that){
that.$router.push('/areas/');
}, 3000);
});
但是我收到这个错误:
Uncaught TypeError: Cannot read property '$router' of undefined
有人可以帮忙吗?
不要将 that
作为匿名函数的参数传递给 setTimeout
。
这样做有效地在匿名函数的范围内重置了 that
,因为您再次将其定义为函数的参数。该函数从未被赋予参数,因此它是 undefined
,这意味着当它尝试访问 $router
属性 时 that
是 undefined
。
您可以使用 BeforeRouteLeave 函数,然后在该函数中描述 setTimeout。