是否有选择避免在下面的代码中重复this.$router.go()
并运行一段代码,无论结果如何?
await axios.put(`/user/` + this.data.id, this.user)
.then((response) => {
this.$router.go();
})
.catch((error) => {
this.$router.go();
});
在Axios中,您可以按照official docs这样操作>
axios.get('url')
.then(function (response) {
// handle success
})
.catch(function (error) {
// handle error
})
.then(function () {
// always executed
this.$router.go();
});
您可以使用Promise的finally
方法:
您可以提前将其放入命名函数中: