vue3中实现关闭浏览器清除localStorage内容,刷新不清空

  • Post author:
  • Post category:vue




vue3中实现关闭浏览器清除localStorage内容,刷新不清空

在App.vue中

<script setup lang="ts">
import { onMounted } from "vue";
onMounted(() => {
	window.onload = function () {
		if (!window.sessionStorage["login"]) {
			// 关闭浏览器时清空
			//window.localStorage.removeItem("userInfo"); 或者写这俩
			//window.localStorage.removeItem("token");
			globalStore.setToken("");
			globalStore.setUserInfo("");
		} 
	};
	window.onunload = function () {
		window.sessionStorage["login"] = true;
	};
	window.onbeforeunload = function () {
		window.sessionStorage["login"] = true;
	};
});
</script>



版权声明:本文为weixin_46858417原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。