<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>localStorage</title>
<!-- 引入样式 -->
<link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
</head>
<body>
<div id="app">
<h1 class="container">localStorage</h1>
<el-select v-model="themeVal" placeholder="请选择" size="mini" style="width: 100px;" @change="changeTheme">
<el-option v-for="item in themeList" :key="item.value" :label="item.label" :value="item.value"> </el-option>
</el-select>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.13/dist/vue.js"></script>
<script src="https://unpkg.com/element-ui/lib/index.js"></script>
<script>
//localStorage方法
let store = {
save(key, value) {
localStorage.setItem(key, JSON.stringify(value));
},
fetch(key) {
return JSON.parse(localStorage.getItem(key)) || [];
}
}
let theme = store.fetch('theme'); //从store获取主题
new Vue({
el: '#app',
data: {
//主题
themeVal: theme,
themeList: [{
value: "light",
label: "浅色"
},
{
value: "dark",
label: "深色"
}
]
},
methods: {
//修改主题
changeTheme() {
store.save('theme', this.themeVal); //保存到store
}
}
});
</script>
</body>
</html>