赞
踩
登录时将后端传来的用户名保存在localStorage
localStorage.setItem('username',res.data.username)
在前端登出
logout(){
//全清 不建议使用
//window.localStorage.clear()
//只清除username
localStorage.removeItem('username')
this.username = '';
this.$router.push('/login')
},
dome
<template> <div class="home-container"> <div v-if="username!=''"> 欢迎您:{{ username }}</div> <el-button type="info" @click="logout">退出</el-button> </div> </template> <script> export default { data(){ return{ username:'', } }, mounted() { let uname = localStorage.getItem('username') //说明未登录或已经登出 if(uname==null){ this.username = '' }else{ this.username = uname } }, created(){ }, methods:{ logout(){ //全清 // window.localStorage.clear() localStorage.removeItem('username') this.$router.push('/login') }, } } </script>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。