Das*_*rau 7 javascript single-page-application vue.js vue-router
我目前正在练习 VueJs 并制作一些示例应用程序。我正在制作一个非常简单的应用程序,它基本上只是一个登录页面,用户将在其中放置他们的凭据并访问他们的个人资料。但是,如果用户未登录(即他们尝试通过手动将 url 更改为 /profile 来访问),我想不出一种方法来限制对个人资料部分的查看
该应用程序非常简单,它只是使用 JS 和引导程序。
如果用户未登录并尝试访问个人资料页面,是否有办法立即将用户重定向回登录屏幕?
您可以使用https://router.vuejs.org/guide/advanced/navigation-guards.html beforeEach检查当前用户是否已登录并执行您需要执行的操作:)。
您的路线:
...
{
path:'/profile',
meta:{guest:false},
component:ProfileComponent
},
...
Run Code Online (Sandbox Code Playgroud)
例子 :
router.beforeEach((to, from, next) => {
if (!to.meta.guest) {
// check if use already logged
// if true then go to home
return next({path:'/'}); // '/' is home page for example
// else then continue to next()
}
return next();
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4617 次 |
| 最近记录: |