前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >vue2自定义指令的权限设计(0->1)

vue2自定义指令的权限设计(0->1)

作者头像
yma16
发布2023-11-16 16:38:08
1310
发布2023-11-16 16:38:08
举报
文章被收录于专栏:前端javascript前端javascript

背景

阅读若依前端低代码看见v-permission时去了解vue的directive自定义指令时发现,可注入属性,有点像v-bind的用法。

新建一个vue项目(webpack 模板)

代码语言:javascript
复制
# vue init webpack yma16-study
# cd yma16-study
# npm run dev

自定义yma16指令

自定义yma16Auth

代码语言:javascript
复制
const yma16Auth = {
  inserted (el, binding, vnode) {
    const { value } = binding
    console.log(binding)
    console.log(el)
    const {visible} = value
    // visible false 移除节点
    if (!visible) {
      console.log('remove', el)
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
}

在main.js配置

代码语言:javascript
复制
const authConfig = {
  isAuthor: true
}
const unAuthConfig = {
  isAuthor: false
}
const yma16Auth = {
  inserted (el, binding, vnode) {
    const { value } = binding
    console.log(binding)
    console.log(el)
    const {visible} = value
    // visible false 移除节点
    if (!visible) {
      console.log('remove', el)
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
}
const yma16UnAuth = {
  inserted (el, binding, vnode) {
    const { value } = binding
    console.log('value', value)
    console.log('binding', binding)
    // 未授权 移除节点
    if (!unAuthConfig.isAuthor) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
}

export {authConfig, yma16Auth, yma16UnAuth, unAuthConfig}

整体的布局设计

代码语言:javascript
复制
<template>
  <div class="container">
    <h1>{{ msg }}</h1>
    <el-switch
      v-model="yma16Auth.visible"
      active-text="显示"
      inactive-text="隐藏"
    >
    </el-switch>
      <div>

        <el-button type="primary" @click="reRenderBtn" style="margin:10px 0">
          重新强制渲染
        </el-button>
      </div>
    <div>
       <span style="margin:10px 0">
        授权显示的按钮 v-yma16Auth
      </span>
    </div>
      <div v-if="refreshBtn">
        <el-button v-yma16Auth="yma16Auth" type="primary" :visiable="yma16Auth.visible">
          v-yma16Auth
        </el-button>
      </div>
  </div>
</template>

<script>
export default {
  name: 'Index',
  data () {
    return {
      msg: '自定义指令 directive',
      yma16Auth: {
        visible: true,
        value: false
      },
      refreshBtn: true
    }
  },
  methods: {
    reRenderBtn () {
      // this.$forceUpdate()
      console.log('yma16Auth', this.yma16Auth)
      this.refreshBtn = false
      setTimeout(() => { this.refreshBtn = true }, 500)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
h1,
h2 {
  font-weight: normal;
}
ul {
  list-style-type: none;
  padding: 0;
}
li {
  display: inline-block;
  margin: 0 10px;
}
a {
  color: #42b983;
}
</style>

效果

隐藏

显示

结尾

感谢你的阅读!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2023-03-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 数据结构框架学习 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档