vue?退出登錄?清除localStorage的問題
vue 退出登錄 清除localStorage
在vue登錄的時(shí)候我們會(huì)保持狀態(tài) 如下:
methods: {
login(){
this.axios.post('users/login/',this.form).then(res=>{
console.log(res.data)
if(res.data.code == 200){
localStorage.setItem('userid',res.data.userid)
localStorage.setItem('username',res.data.username)
localStorage.setItem('mobile',res.data.mobile)
this.$router.push({
name:'Index'
})
}
})
}
},
此時(shí) 瀏覽器中會(huì)將狀態(tài)保存

當(dāng)退出賬號(hào) 我們就需要 清除狀態(tài)保持
<template>
<div>
<a @click="exit" >退出</a>
</div>
</template>
<script>
import axios from 'axios'
export default {
props: {
passUser:{
}
},
data() {
return {
}
},
methods: {
exit(){
// 清除狀態(tài)保持
window.localStorage.clear()
// 狀態(tài)保持清除后刷新頁面
window.location.reload()
}
},
created() {
}
}
</script>
<style scoped>
</style>
vue 登錄后無操作半小時(shí)后自動(dòng)清除登錄狀態(tài)
在項(xiàng)目的頁面入口文件App.vue文件中監(jiān)聽用戶最后一次操作鼠標(biāo)、鍵盤或滾動(dòng)事件:
<template>
<div id="app">
<router-view />
</div>
</template>
<script>
// 登錄狀態(tài)使用store插件保存在客戶端的localStorage中
import storage from 'store'
export default {
name: 'App',
computed: {
token () {
return storage.get('TOKEN')
},
uid () {
return storage.get('UID')
},
userInfo () {
return storage.get('USER_INFO')
}
},
mounted () {
document.onmousemove = this.debounce(this.resetStatus, 3000)
document.onkeydown = this.debounce(this.resetStatus, 3000)
document.onscroll = this.debounce(this.resetStatus, 3000)
},
methods: {
// 使用防抖,對(duì)于短時(shí)間內(nèi)(此處是3s)連續(xù)觸發(fā)的事件,只執(zhí)行最后一次
debounce (fn, delay) {
let timer = null
return function () {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(fn, delay)
}
},
resetStatus () { // 重置store插件自動(dòng)清除時(shí)間
if (this.token) {
storage.set('TOKEN', this.token, new Date().getTime() + 30 * 60 * 1000)
storage.set('UID', this.uid, new Date().getTime() + 30 * 60 * 1000)
storage.set('USER_INFO', this.userInfo, new Date().getTime() + 30 * 60 * 1000)
}
}
}
}
</script>
<style lang="less" scoped>
#app {
min-height: 100vh;
min-width: 1200px;
margin: 0 auto;
}
</style>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue通過滾動(dòng)行為實(shí)現(xiàn)從列表到詳情,返回列表原位置的方法
今天小編就為大家分享一篇vue通過滾動(dòng)行為實(shí)現(xiàn)從列表到詳情,返回列表原位置的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-08-08
vuejs+element-ui+laravel5.4上傳文件的示例代碼
本篇文章主要介紹了vuejs+element-ui+laravel5.4上傳文件的示例代碼,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Vue單頁面應(yīng)用做預(yù)渲染的方法實(shí)例
vue是一個(gè)單頁面的應(yīng)用,這導(dǎo)致一些爬蟲和百度無法搜索到,如果你想針對(duì)你應(yīng)用的其中某些頁面進(jìn)行SEO優(yōu)化,讓他們可以被爬蟲和百度搜索到,你可以進(jìn)行預(yù)渲染操作,下面這篇文章主要給大家介紹了關(guān)于Vue單頁面應(yīng)用做預(yù)渲染的相關(guān)資料,需要的朋友可以參考下2021-10-10
vue3動(dòng)態(tài)路由+菜單欄的實(shí)現(xiàn)示例
在后臺(tái)管理系統(tǒng),可以根據(jù)登錄用戶的不同返回不同路由,頁面也會(huì)根據(jù)這些路由生成對(duì)應(yīng)的菜單,本文主要介紹了vue3動(dòng)態(tài)路由+菜單欄的實(shí)現(xiàn)示例,感興趣的可以了解一下2024-04-04
el-select與el-tree結(jié)合使用實(shí)現(xiàn)樹形結(jié)構(gòu)多選框
我們在實(shí)際開發(fā)中需要用到下拉樹,elementUI是沒有這個(gè)組件的,我們就要自己去寫了,下面這篇文章主要給大家介紹了關(guān)于el-select與el-tree結(jié)合使用實(shí)現(xiàn)樹形結(jié)構(gòu)多選框的相關(guān)資料,需要的朋友可以參考下2022-10-10
vue中$router.back()和$router.go()的用法
這篇文章主要介紹了vue中$router.back()和$router.go()的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-09-09

