用JSON的parse 和 stringify 可以实现缓存数组
uni登录模板的例子
const USERS_KEY = 'USERS_KEY';
const STATE_KEY = 'STATE_KEY';
const getUsers = function () {
let ret = '';
ret = uni.getStorageSync(USERS_KEY);
console.log("ret",ret);
if (!ret) {
ret = '[]';
}
return JSON.parse(ret);
}
const addUser = function (userInfo) {
let users = getUsers();
console.log("users",users);
users.push({
account: userInfo.account,
password: userInfo.password
});
uni.setStorageSync(USERS_KEY, JSON.stringify(users));
}
export default {
getUsers,
addUser
}