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

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.mrgr.cn/news/30363.html

相关文章:

  • vue h5 蓝牙连接 webBluetooth API
  • 对 JavaScript 原型的理解
  • ELK企业级日志分析系统
  • 从工厂打螺丝到数据库专家(上)
  • 把设计模式用起来!(4) 用不好模式?之原理不明
  • FortiGate 透明模式下配置注意事项和故障排错技巧
  • 维钧团队与广东能源集团携手共创未来
  • 华为、思科、新华三,三大厂商认证到底选择哪一个?
  • 力扣438 找到字符串中所有字母异位词 Java版本
  • 设计模式之外观设计模式
  • 教师专属:高效查询学生考试成绩系统 - 立即体验吧
  • C++:动态内存分配(new、delete 相比 malloc、free的优势)与运算符重载
  • 完美解决 Async/await 不按预期工作 的正确解决方法,亲测有效!!!
  • python+flask+mongodb+vue撸一个实时监控linux服务资源的网站
  • 从 InnoDB 到 Memory:MySQL 存储引擎的多样性
  • 更换UFS绑定固件与“工程固件”的区别 小米10s机型更换cpu绑定包对比 写入以及修复基带
  • 无人机 PX4 飞控 | EKF 使用传感器汇总与添加传感器方法
  • Pytorch使用集成可形变卷积构建网络并导出onnx模型
  • (六)WebAPI方法的调用
  • 学习风格的类型