utils中的目录解构如下(utils在src目录下),以深拷贝为例:
deepClone文件的代码如下:
function deepClone(substance) {
if (typeof substance !== 'object' || substance == null) {
return substance;
}
const result = Array.isArray(substance) ? [] : {};
for (const key in substance) {
if (Object.prototype.hasOwnProperty.call(substance, key)) {
if (typeof substance[key] === 'object' && substance[key] !== null) {
result[key] = deepClone(substance[key]);
} else {
result[key] = substance[key];
}
}
}
return result;
}
export { deepClone }
在需要引入的vue文件中导入:
import {deepClone} from '@/utils/deepClone'
在需要使用的地方直接引用,不需要加this.
//this.list 为深拷贝的数据,obtainList为深拷贝后的值
let obtainList = deepClone(this.list)
标签:vue,utils,deepClone,substance,引用,key,拷贝,result
From: https://blog.51cto.com/u_16372215/8694774