// 序列化
function mapReplacer(key, value) {
if (value instanceof Map) {
return { dataType: 'Map', value: Array.from(value.entries()) };
}
return value;
}
// const map = new Map([['date', new Date()]]);
// const jsonStr = JSON.stringify(map, mapReplacer); // 输出带类型标记的JSON:ml-citation{ref="1" data="citationList"}
function mapReviver(key, value) {
if (value?.dataType === 'Map') {
return new Map(value.value);
}
if (value?.__type === 'Date') return new Date(value.value);
return value;
}
// const restoredMap = JSON.parse(jsonStr, mapReviver); // 恢复为Map实例:ml-citation{ref="1" data="citationList"}
//user.store
// persist: true //会把Map数据结构改成{} ,必须手动指定序列化方法和反序列化方法
persist: {
// key: 'dyApp', // 存储的键名,默认为 store 的 id
serializer: {
serialize: (value) => {
return JSON.stringify(value,mapReplacer)
},
deserialize: (value) => {
let deCache = JSON.parse(value, mapReviver)
return deCache
}
}
}