Vuejs2 如何在Vue中保护单页应用程序的私人区域?(最佳做法)
我是Vue和SPA的新手,所以这可能是一个简单的问题。。。要限制对Vue中某些页面的访问,我使用以下代码。它使用路由定义中的元数据明确允许访问页面:Vuejs2 如何在Vue中保护单页应用程序的私人区域?(最佳做法),vuejs2,jwt,vue-router,Vuejs2,Jwt,Vue Router,我是Vue和SPA的新手,所以这可能是一个简单的问题。。。要限制对Vue中某些页面的访问,我使用以下代码。它使用路由定义中的元数据明确允许访问页面: router.beforeEach((to, from, next) => { const isNotFound = to.matched.some(record => record.path == "*") if (isNotFound) { return next(
router.beforeEach((to, from, next) => {
const isNotFound = to.matched.some(record => record.path == "*")
if (isNotFound) {
return next()
}
else {
const isPublic = to.matched.some(record => record.meta.public)
const onlyWhenLoggedOut = to.matched.some(record => record.meta.onlyWhenLoggedOut)
const loggedIn = !!TokenService.getToken()
if (!isPublic && !loggedIn) {
return next({
path:'/login',
query: {redirect: to.fullPath} // Store the full path to redirect the user to after login
})
}
next()
} //else
} // router.beforeEach
在上面的例子中,我从令牌服务获取并测试令牌。我的后端API受JWT保护,所以我不担心暴露数据,但在客户端SPA中,似乎不可能绝对保护页面。(在上述情况下,我只需要在if语句中注入true
保护页面客户端的最佳实践是什么?我做错了吗