在src下的新建的utils文件夹中新建index.js文件,将封装代码写入:
/**
* 深拷贝
* @obj {Object} 被复制的对象
* @target {Object} 目标对象,默认为空对象
* @return {Object}
*/
function clone(obj, target) {
target = target || {}
for (var i in obj) {
if (typeof obj[i] === 'object') {
target[i] = (obj[i].constructor === Array) ? [] : {}
clone(obj[i], target[i])
} else {
target[i] = obj[i]
}
}
return target
}
//封装好之后导出
export {
clone
}
在页面中引入,方法中使用
import { clone } from '@/utils'