当前位置: 首页 > article >正文

vue2.0+ts注册全局函数和几个递归查找

vue2.0+ts注册全局函数和几个递归查找

  • 一、main.ts

一、main.ts

// 定义你的全局函数,判断是否有按钮权限
interface Item {
  label: string;
  checked: number;
  [k: string]: any;
}
// 获取按钮时候权限
function globalLable(arr: Item[], label: string): boolean {
  for (const item of arr) {
    if (item.label === label) {
      return item.checked == 1 ? true : false;
    }
  }
  return false;
}
// 判断是否显示按钮
function globalShow(arr: Item[], label: string): boolean {
  for (const item of arr) {
    if (item.label == label) {
      return true;
    }
  }
  return false;
}
// 判断当子菜单子菜单
function globalMenu(arr: Item[], label: string): Item[] {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].label === label) {
      return arr[i].children || [];
    }
    if (arr[i].children) {
      const siblings = globalMenu(arr[i].children, label);
      if (siblings.length > 0) {
        return siblings;
      }
    }
    // }
  }
  return [];
}
// 判断当前子菜单子按钮
function globalButton(arr: Item[], label: string): Item[] {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i].label === label) {
      if (arr[i].operations) {
        return arr[i].operations;
      }
    }
    if (arr[i].children.length > 0) {
      const foundSubMenu = globalButton(arr[i].children, label);
      if (foundSubMenu.length > 0) {
        return foundSubMenu;
      }
    }
  }
  return [];
}

// 在Vue的原型上添加这个全局函数
declare module 'vue/types/vue' {
  interface Vue {
    $l: typeof globalLable;
    $s: typeof globalShow;
    $m: typeof globalMenu;
    $b: typeof globalButton;
  }
}
// 在你的Vue实例化之前添加这个方法
Vue.prototype.$l = globalLable;
Vue.prototype.$s = globalShow;
Vue.prototype.$m = globalMenu;
Vue.prototype.$b = globalButton;

如何使用 this.$f(a,b)就行了

结束啦~
放一张美照
在这里插入图片描述


http://www.kler.cn/news/311843.html

相关文章:

  • 前端vue-关于标签切换的实现
  • 【Verilog学习日常】—牛客网刷题—Verilog快速入门—VL24
  • 基于CNN的10种物体识别项目
  • Spark-ShuffleWriter-UnsafeShuffleWriter
  • react是什么?
  • 数据结构、STL
  • 私域直播平台带源码
  • FRIDA-JSAPI:Java使用
  • leetcode:字符串中的第一个唯一字符
  • YOLOv10轻量化快速涨点之改进AKConv
  • Linux:vim编辑技巧
  • 【STL】priority_queue 基础,应用与操作
  • C++ 头文件 .h和.hpp的介绍与区别
  • 深度学习常见面试题及答案(11~15)
  • [Linux]自定义shell详解
  • HTML/CSS/JS学习笔记 Day4(CSS--C1 选择器声明)
  • .dav视频文件及格式转换
  • HashMap高频面试知识点
  • 【Ubuntu】ubuntu如何使用ufw(Uncomplicated Firewall)管理防火墙?一文带你学会!
  • Ubuntu-24.04中Docker-Desktop无法启动
  • 怎么操作使http变成https访问?
  • 力扣 中等 2300.咒语和药水的成功对数
  • OpenAI最新发布的o1-preview模型,和GPT-4o到底哪个更强?
  • 驱动---动态模块编译
  • win11开始按钮点不开(已解答)
  • sql中拼接操作
  • 从“治理”到“智理”,看大模型如何赋能智慧政务
  • Linux 信号的产生
  • Windows本地pycharm使用远程服务器conda虚拟环境
  • 【Android】Handler用法及原理解析