vue封装工具类方法更新

1.新建一个utils文件夹,在文件里新建文件toolClass.js,如下文

export default class toolClass{

  // 非空判断

  static isNotEmpty(value) {

    if (value == null || value == undefined || typeof (value) == 'undefined') return false

    if (Object.keys(value).length === 0) {

      return false

    } else {

      return true

    }

  };

  //字典码值转换

  static codeToValue(code, dict) {

    return dict[code];

  }

  // 将字典转换为数组

  static dictToArray(dict) {

    const arr = [];

    for (const code in dict) {

      if (dict.hasOwnProperty(code)) {

        arr.push({ label: dict[code], value: code });

      }

    }

    return arr;

  }

  //键盘enter事件

  static enter(e, callback) {

    if (e.keyCode !== 13) return

    callback()

  }

  //生成uuid方法

  static uuid() {

    let s = [];

    const hexDigits = "0123456789abcdef";

    for (let i = 0; i < 36; i++) {

      s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);

    }

    s[14] = "4";

    s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);

    s[8] = s[13] = s[18] = s[23] = "-";

    const uuid = s.join("");

    return uuid;

  }

  // 树形结构数据根据树id查找节点;proId属性名称也就是id,但是有的不用id所以这里用proId传变量,proChild子节点列表:有的命名child,有的命名children,所以这里用变量传值

  static findNodeById(tree, id, proId, proChild) {

    for (const node of tree) {

      if (node[proId] === id) {

        return node

      }

      if (node[proChild].length > 0) {

        const foundNode = toolClass.findNodeById(node[proChild], id, proId, proChild)

        if (foundNode) {

          return foundNode

        }

      }

    }

    return null

  }

 // 平铺数据转树-- list树列表,propId为list列表中单个对象自己id,parentPropId为对象的父类id,就是id与parentId

  static convertToTree(list, propId, parentPropId) {

    console.log(list, 'list============')

    const map = new Map()

    const result = []

    // 将 list 转换成以节点 ID 为键的映射表

    for (const node of list) {

      node.children = []

      map.set(node[propId], node)

    }

    // 构建树形结构数据

    for (const node of list) {

      if (node && node[parentPropId]) {

        const parent = map.get(node[parentPropId])

        if (parent) {

          parent.children.push(node)

        } else {

          result.push(node)

        }

      } else {

        result.push(node)

      }

    }

    return result

  }

//数组去重

}

2.挂载全局在main.js中引入

import toolClassfrom '@/utils/toolClass';

Vue.prototype.$toolObj = toolObj

3如果单独在某个页面使用,页面直接引用

import toolObj from '@/utils/toolObj';

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值