gpt4 book ai didi

vue.js - 从路由导航时如何在vue中调用函数?

转载 作者:行者123 更新时间:2023-12-05 06:13:42 25 4
gpt4 key购买 nike

我正在尝试调用一个函数,在用户离开当前页面时清除本地存储。我将如何做到这一点?我尝试使用 destroyed() 生命周期 Hook ,但它没有用。使用 beforeRouteLeave() 会是一个很好的解决方案吗?我将如何在我的路由文件中实现它?

我的路线:

  {
path: "/success",
name: "Success",
component: () =>
import("../views/Success.vue"),
},

我在当前成功页面上的钩子(Hook):

  destroyed() {
window.localStorage.removeItem("intent");
},

我尝试使用 beforeRouteLeave

  beforeRouteLeave: function(to, from, next) {
window.localStorage.removeItem("intent");
next();
},

我的 Hook

let intent = window.localStorage.getItem(intent);
// const product = window.localStorage.getItem(product);

axios
.get("http://localhost:5000/pay/confirm", {
params: {
intent: intent
}
})
.then(res => {
console.log(res.data.status);
if (res.data.status == "succeeded") {
console.log(res.data.status);
this.confirmPayment();
} else {
this.paid = false;
}
console.log(this.item);
});
},

最佳答案

你可以尝试为你设置(全局/页面)观察者,当你路由对象改变时它会做一些事情

watch:{
$route function(to, from){
// do something
}
}

关于vue.js - 从路由导航时如何在vue中调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63195830/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com