<template> <el-button type="primary" @click="ls(++i)">修改localStorage的变量为{{ i }}</el-button> </template> <script> export default { data() { return { i: 0 } }, created() { //监听localStorage的变化 onstorage = (e) => { if (e.key === '被监听的变量') { this.i = e.newValue;//同步监听后的值 console.log(e.key, e.newValue); } }; }, methods: { ls(d) { localStorage.被监听的变量 = d;//修改localStorage的变量 }, }, }; </script>