首页 > 其他分享 >通过自定义指令控制按钮权限

通过自定义指令控制按钮权限

时间:2023-07-12 14:49:07浏览次数:56  
标签:el const 自定义 label key 按钮 权限

目录

常见写法

通常控制按钮显示与否,会采用v-if或者v-show来控制,可能会写成以下形式,在通过动态的改变 active 变量的值,控制按钮的显示状态,

<template>
  <div>
    <button v-if="(active = '1')">按钮一</button>
    <button v-else-if="(active = '2')">按钮二</button>
    <button v-else-if="(active = '3')">按钮三</button>
    <button v-else-if="(active = '4')">按钮四</button>
    <button v-else="(active = '6')">按钮五</button>
  </div>
</template>
<script lang="ts" setup>
  import { ref } from "vue";

  const active = ref("1"); // 此时页面显示 按钮一
</script>

进一步优化代码,第二种形式也有可能会通过一个集合遍历的方式去渲染,减少组件的重复编写,因为v-forv-if不建议作用于同一个组件上,因此包裹了一层 template 组件

<template>
  <template v-for="(item, index) in btns">
    <!-- 此时仅显示 按钮一 -->
    <button v-if="item.key == active">{{ item.label }}</button>
  </template>
</template>
<script lang="ts" setup>
  import { ref } from "vue";

  const active = ref("1");
  const btns = ref([
    { key: "1", label: "按钮一" },
    { key: "2", label: "按钮二" },
    { key: "3", label: "按钮三" },
    { key: "4", label: "按钮四" },
    { key: "5", label: "按钮五" },
  ]);
</script>

自定义指令小试牛刀

<template>
  <div>
    <span v-color="'red'">测试自定义指令-这段文字将会是红色的</span>
  </div>
</template>
// main.ts
import { createApp } from "vue";
import App from "./App.vue";

const app = createApp(App);
app.directive("color", (el, binding) => {
  el.style.color = binding.value;
});

最终版本通过自定义指令来控制多个按钮的权限操作

<template>
  <div>
    <!-- <button v-display-key="'1'">按钮一</button>
    <button v-display-key="'2'">按钮二</button>
    <button v-display-key="'3'">按钮三</button>
    <button v-display-key="'4'">按钮四</button>
    <button v-display-key="'5'">按钮五</button> -->

    <!-- 优化 -->
    <!-- 此时会显示 按钮二、按钮四 -->
    <template v-for="(item, index) in btns">
      <button v-display-key="item.key">{{ item.label }}</button>
    </template>
  </div>
</template>
<script lang="ts" setup>
  const btns = ref([
    { key: "1", label: "按钮一" },
    { key: "2", label: "按钮二" },
    { key: "3", label: "按钮三" },
    { key: "4", label: "按钮四" },
    { key: "5", label: "按钮五" },
  ]);
</script>
// main.ts
import { createApp } from "vue";
import App from "./App.vue";

const app = createApp(App);

app.directive("display-key", (el, binding) => {
  let displayKey = binding.value;
  if (displayKey) {
    let hasKey = checkArray(displayKey);
    if (!hasKey) {
      el.parentNode && el.parentNode.removeChild(el);
    }
  } else {
    throw new Error("need a key value");
  }
});

function checkArray(key) {
  let arr = ["2", "4", "6", "8"]; // 需要显示的 按钮的key值
  return arr.indexOf(key) > -1;
}

标签:el,const,自定义,label,key,按钮,权限
From: https://www.cnblogs.com/echoyya/p/17476312.html

相关文章

  • springboot 自定义整合caffeine 本地缓存
    1、自定义缓存配置类@Data@ConfigurationProperties(prefix="page.cache")publicclassPageCacheProperties{privateCaffeineConfigPropertiescaffeine=newCaffeineConfigProperties();//本地缓存配置privatePageCacheAsyncExecutorConfigpool=newP......
  • vue3自定义指令 拖拽 与拖拽变大小
    directives:{drag:{mounted:(el,binding)=>{constdragDom=el;conststy=dragDom.currentStyle||window.getComputedStyle(dragDom,null);el.parentElement.style.cursor='move';......
  • 基于JavaFX的扫雷游戏实现(五)——设置和自定义控件
      它来了它来了,最后一期终于来了。理论上该讲的全都讲完了,只剩下那个拖了好几期的自定义控件和一个比较没有存在感的设置功能没有讲。所以这次就重点介绍它们俩吧。  首先我们快速浏览下设置的实现,上图:  然后是控制器代码:SettingsController.javapackagecontrollers;......
  • 解决root用户对HDFS文件系统没有权限的问题
    解决root用户对HDFS文件系统没有权限的问题说明:HDFS文件系统的目录基本都属于supergroup超级用户组,所以就把用户添加到该用户组,即可解决很多权限问题。第一步:在Linux执行如下命令增加supergroup用户组groupaddsupergroup第二步:然后将用户root增加到supergroup用户组......
  • React中编写操作树形数据的自定义Hook
    什么是Hookhook即为钩子,是一种特殊的函数,它可以让你在函数式组件中使用一些react特性,目前在react中常用的hook有以下几类useState:用于在函数组件中定义和使用状态(state)。useEffect:用于在函数组件中处理副作用,也可以模拟react生命周期useContext:用于在函......
  • mapbox添加自定义控件
    需要定义一个类,然后至少重写实现onAdd、onRemove方法,示例如下<template><divref="changeViewRef"@click="changeView"class="changeViewmapboxgl-ctrl"><el-tooltipclass="box-item"effect="dark"......
  • 自定义筛选AutoFilter
    AutoFilter语法:expression.AutoFilterVBA直接输入这个是在自动筛选和关闭来回切换。AutoFilter.FilterMode属性如果工作表处于自动筛选筛选器模式,则返回 True。只读 Boolean。表达 一个代表 AutoFilter 对象的变量。语法:expression.FilterModee.g:SubClearFilter()......
  • VBA自定义排序
    SortField.clear方法清除所有 SortFields 对象。SortFields.Add方法创建新的排序字段,并返回一个SortFields 对象。语法:expression.SortFields.add(key、SortOn、 Order、 CustomOrder、 DataOption)'Key:指定排序字段的范围或单元格。'SortOn:指定排序方式。例如xl......
  • SignalR 外部调用自定义Hub类的方法,Clients为null
    这是因为外部调用的类的对象和你连接的Hub类的对象,这两个对象不!一!样!解决方法在自定义的Hub类中,注入IHubContext对象,然后在方法中调用IHubContext对象来向前端推送数据publicclassDataHub:AbpCommonHub,ITransientDependency{publicIOnlineClientManag......
  • 自定义参数类型断言装饰器
    代码frominspectimportsignaturefromfunctoolsimportwrapsdeftypeassert(*ty_args,**ty_kwargs):defdecorate(func):ifnot__debug__:returnfuncsig=signature(func)#获取函数签名bound_types=sig.bind_parti......