export default {
data() {
return {
title: "",
status: false,
list: [],
};
},
methods: {
//添加
add() {
//添加一个变量 获取本地
let lists = JSON.parse(localStorage.getItem("list")) || [];
//筛选数据
let result = lists.find((item) => {
return item.title == this.title;
});
//判断数据 如果一样就return
if (result) {
alert("内容不能重复");
return;
}
//添加一个变量
let obj = {
title: this.title,
};
//将数据放进list
this.list.push(obj);
//存入本地
localStorage.setItem("list", JSON.stringify(this.list));
},
//删除
del(index) {
this.list.splice(index, 1);
//重新保存本地
localStorage.setItem("list", JSON.stringify(this.list));
},
},
created() {
if (localStorage.getItem("list")) {
//将本地的数据保存到list
this.list = JSON.parse(localStorage.getItem("list")) || [];
}
},
};
vue本地存储
最新推荐文章于 2025-02-28 13:44:45 发布