0
点赞
收藏
分享

微信扫一扫

vue中引用utils中的方法

utils中的目录解构如下(utils在src目录下),以深拷贝为例:

vue中引用utils中的方法_数据

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)

举报

相关推荐

0 条评论