[英]How to hide login route navigation element after user has logged in using token authentication?
On login, I save the token in the local storage using localStorage.setItem('token', token);
登录时,我使用localStorage.setItem('token', token);
将令牌保存在本地存储中localStorage.setItem('token', token);
but now I would like to hide the navigation elements that leads to the login and signup routes. 但现在我想隐藏导致登录和注册路线的导航元素。 These are my routes: 这些是我的路线:
<template>
<div class="nav-header">
<div class="wrapper">
<ul class='nav-ul'>
<router-link to="/" tag='li' active-class='active' exact><li><a>Home</a></li></router-link>
<router-link to="/signup" tag='li' active-class='active' exact><li><a>Sign Up</a></li></router-link>
<router-link to="/signin" tag='li' active-class='active' exact><li><a>Sign In</a></li></router-link>
</ul>
</div>
</div>
</template>
I assume I need to check if I have a stored token in my local storage and hide the routes if I do. 我假设我需要检查本地存储中是否存储了令牌,如果有的话,请隐藏路由。 Any tips and pointers would be greatly appreciated. 任何提示和指示,将不胜感激。
You can define a computed
property in your component, for example, isLoggedIn
which returns a boolean
value. 您可以在组件中定义一个computed
属性,例如isLoggedIn
,它返回一个boolean
值。
computed: {
isLoggedIn() {
return !!window.localStorage.getItem('token')
}
}
You can then use the v-if
directive in the routes that you want to hide. 然后,可以在要隐藏的路由中使用v-if
指令。
<template>
<div class="nav-header">
<div class="wrapper">
<ul class='nav-ul'>
<router-link to="/" tag='li' active-class='active' exact><li><a>Home</a></li></router-link>
<router-link to="/signup" v-if="!isLoggedIn" tag='li' active-class='active' exact><li><a>Sign Up</a></li></router-link>
<router-link to="/signin" v-if="!isLoggedIn" tag='li' active-class='active' exact><li><a>Sign In</a></li></router-link>
</ul>
</div>
</div>
</template>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.