[个人用] 常用工具
对象合并
const obj = {
id: null,
count: null,
completed: null
};
// 假设这是接口返回的对象
const data = {
id: 1,
count: 2,
completed: 3,
otherParam: {
// 返回的其他无用字段
}
};
Object.keys(obj).forEach(key => {
obj[key] = data[key];
});
无用字段不会进入obj
对象中
vue2.7 以下使用 volar
// tsconfig.json
{
"compilerOptions": {
"baseUrl": ".",
"allowJs": true,
"paths": {
"@/*": ["src/*"]
}, // 使vscode识别@路径
"outDir": "./",
"jsx": "preserve"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"],
"vueCompilerOptions": {
"target": 2
// "target": 2, // For Vue version <= 2.6.14
}
}
下载 Blob 文件
function $downloadBlob(blob, fileName) {
const url = URL.createObjectURL(blob);
const hyperLink = document.createElement("a");
hyperLink.setAttribute("href", url);
hyperLink.setAttribute("download", fileName);
hyperLink.click();
hyperLink.remove();
}
睡眠函数
function $sleep(delay) {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, delay);
});
}
过滤参数
function $filterParam(params) {
Object.keys(params).forEach(keys => {
if (["", null, undefined].includes(params[keys])) {
Reflect.deleteProperty(params, keys);
}
});
return params;
}