【发布时间】:2021-05-04 22:33:20
【问题描述】:
我正在使用 Vue 和 axios 之类的
[...]
import VueAxios from "vue-axios";
import axios from "@/axios";
createApp(App)
.use(store)
.use(router)
.use(VueAxios, axios)
.mount("#app");
[...]
像this.axios... 一样在全球范围内都非常有效。我也在使用 Passport 进行身份验证,在我的受保护路由中,我想调用我的 Express-endpoint .../api/is-authenticated 来检查用户是否已登录。
为了完成这项工作,我想使用 beforeRouteEnter-navigation 守卫,但不幸的是我不能在那里调用它。
现在我正在安装挂钩,感觉不对。有什么解决方案可以让我的代码保持直截了当吗?
我很感激一个提示。谢谢。
编辑:这对我有用。
beforeRouteEnter(to, from, next) {
next((vm) => {
var self = vm;
self
.axios({ method: "get", url: "authenticate" })
.then() //nothing needed here to continue?
.catch((error) => {
switch (error.response.status) {
case 401: {
return { name: "Authentification" }; //redirect
//break;
}
default: {
return false; //abort navigation
//break;
}
}
});
});
【问题讨论】:
标签: vue.js axios vue-component passport.js vue-router