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)就行了
结束啦~
放一张美照