// 保留JSON文件
saveJSONFile = (data, filename) => {
if (typeof data === 'object') {
// data = JSON.stringify(data, null, 4)
// 勾销序列化丑化
data = JSON.stringify(data)
}
// 构建下载对象
const blobURL = new Blob([data], { type: 'text/json' })
const tempLink = document.createElement('a')
tempLink.style.display = 'none';
tempLink.href = window.URL.createObjectURL(blobURL)
tempLink.download = `${filename}.json`
// 模仿点击
document.body.appendChild(tempLink);
tempLink.click();
// 删除DOM、开释对象URL
setTimeout(() => {
document.body.removeChild(tempLink);
window.URL.revokeObjectURL(blobURL);
}, 200)
}
发表回复