Vue 中数据存储主要使用到的对象为 localStorage 监听放大为watch
首先在data 中定义需要监听的值
data() {
return {
title: "我叫路飞",
destation: "要成为海贼王的男人",
list: [],
};
},
开始做数据监听
watch: {
list: {
deep: true,
handler(value) {
localStorage.setItem("todo", JSON.stringify(value));
},
},
title: {
deep: false,
handler(value) {
localStorage.setItem("title", JSON.stringify(value));
},
},
},
当前页加载时取出缓存到本地的值
created() {
this.list = JSON.parse(localStorage.getItem("todo")) || [];
this.title = JSON.parse(localStorage.getItem("title")) || "";
},
本地数据存储就完成了