vue自定义指令设置(vue自定义指令在什么场景使用)

技术vue中自定义指令怎么用小编给大家分享一下vue中自定义指令怎么用,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!1、v-drag需求:鼠标拖动元素思路:元素偏移量 = 鼠标滑动后的坐标 - 鼠标初始

边肖将与您分享如何在vue中使用自定义命令。希望大家看完这篇文章后有所收获。我们一起讨论一下吧!

1、v-drag

要求:用鼠标拖动元素

思考:

元素偏移量=鼠标滑动后的坐标-鼠标最初单击元素时的坐标。首次单击时可见区域中元素的顶部和左侧。

使用可见区域作为边界,并限制在可见区域拖动。

代码:

Vue.directive('drag ',{ 0

插入(El){ 0

letheader=el.querySelector('。dialog _ header’)

header . style . CSS text=';cursor:move'

header . onmousedown=function(e){ 0

//获取当前可见区域的宽度和高度

letclientWidth=document . document element . client width

letclientHeight=document . document element . client height

//获取自己的宽度和高度

letelWidth=El . getboundingclientrect()。宽度

letelhx8=El . getboundingclientrect()。高度

//从当前距离获取可见区域的顶部和左侧

letelTop=El . getboundingclientrect()。顶端

letelLeft=El . getboundingclientrect()。左边的

//点击时获取坐标。

letstartX=e.pageX

letstartY=e.pageY

document . onmousemove=function(e){ 0

//元素偏移量=鼠标滑动后的坐标-鼠标最初点击元素时的坐标。首次单击时可见区域中元素的顶部和左侧。

letmoveX=e.pageX-startX elLeft

letmoveY=e.pageY-startY elTop

//以可见区域为边界,在可见区域内拖动。

if((moveX El width)client width | | moveX 0 | |(moveY El height)client heightn

bsp;|| moveY < 0) {
          return
        }
 
        el.style.cssText += 'top:' + moveY + 'px;left:' + moveX + 'px;'
      }
      document.onmouseup = function () {
        document.onmousemove = null
        document.onmouseup = null
      }
    }
  }
})

2、v-wordlimit

需求:后台字段限制了长度,并且区分中英文,中文两个字节,英文一个字节;所以输入框需要限制输入的字数并且区分字节数,且需回显已输入的字数。

思路:

一个字节的正则/[\x00-\xff]/g

创建包裹字数限制的元素,并定位布局在textarea和input框上

分别计算输入的字符一个字节的有enLen个,两个字节的有cnLen个;用来后面字符串截断处理

当输入的字数超过限定的字数,截断处理;substr(0,enLen+cnLen)

接口更新了输入框的值,或者初始化输入框的值,需要回显正确的字节数

代码:

Vue.directive('wordlimit',{
  bind(el,binding){
    console.log('bind');
    let { value } = binding
    Vue.nextTick(() =>{
      //找到输入框是textarea框还是input框
      let current = 0
      let arr = Array.prototype.slice.call(el.children)
      for (let i = 0; i < arr.length; i++) {
        if(arr[i].tagName=='TEXTAREA' || arr[i].tagName=='INPUT'){
          current = i
        }
      }
   
      //更新当前输入框的字节数
      el.children[el.children.length-1].innerHTML = el.children[current].value.replace(/[^\x00-\xff]/g,'**').length +'/'+value//eslint-disable-line
    })
  },
  update(el,binding){
    console.log('update');
    let { value } = binding
    Vue.nextTick(() =>{
      //找到输入框是textarea框还是input框
      let current = 0
      let arr = Array.prototype.slice.call(el.children)
      for (let i = 0; i < arr.length; i++) {
        if(arr[i].tagName=='TEXTAREA' || arr[i].tagName=='INPUT'){
          current = i
        }
      }
   
      //更新当前输入框的字节数
      el.children[el.children.length-1].innerHTML = el.children[current].value.replace(/[^\x00-\xff]/g,'**').length +'/'+value//eslint-disable-line
    })
  },
  inserted(el,binding){
    console.log('inserted');
    let { value } = binding
 
    //找到输入框是textarea框还是input框
    let current = 0
    let arr = Array.prototype.slice.call(el.children)
    for (let i = 0; i < arr.length; i++) {
      if(arr[i].tagName=='TEXTAREA' || arr[i].tagName=='INPUT'){
        current = i
      }
    }
 
    //创建包裹字数限制的元素,并定位布局在textarea和input框上
    let div = document.createElement('div')
    if(el.children[current].tagName=='TEXTAREA'){//是textarea,定位在右下角
      div.style = 'color:#909399;position:absolute;font-size:12px;bottom:5px;right:10px;'
    }else{
      let styStr = ''
      if(!el.classList.contains('is-disabled')){//input框不是置灰的状态则添加背景颜色
        styStr = 'background:#fff;'
      }
      div.style = 'color:#909399;position:absolute;font-size:12px;bottom:2px;right:10px;line-height:28px;height:28px;'+styStr
    }
 
    div.innerHTML = '0/'+ value
    el.appendChild(div)
    el.children[current].style.paddingRight = '60px'
 
    el.oninput = () =>{
      let val = el.children[current].value
      val = val.replace(/[^\x00-\xff]/g,'**') //eslint-disable-line
      // 字数限制的盒子插入到el后是最后一个元素
      el.children[el.children.length-1].innerHTML = val.length + '/' + value
      if(val.length>value){
        let cnLen = 0 //一个字节的字数
        let enLen = 0 //两个字节的字数
 
        if(val.match(/[^**]/g) && val.match(/[^**]/g).length){
          enLen = val.match(/[^**]/g).length // 计算一个字节的字数
 
          //一个字节两个字节都有的情况
          if((value - val.match(/[^**]/g).length)>0){
            cnLen = Math.floor((value - val.match(/[^**]/g).length)/2)
          }else{
            cnLen = 0
          }
        }else{ //全部两个字节的情况
          enLen = 0
          cnLen = Math.floor(value/2)
        }
 
        if(enLen>value){
          enLen = value
        }
 
        //超过限定字节数则截取
        el.children[current].value = el.children[current].value.substr(0,enLen+cnLen)
 
        //更新当前输入框的字节数
        el.children[el.children.length-1].innerHTML = el.children[current].value.replace(/[^\x00-\xff]/g,'**').length +'/'+value//eslint-disable-line
 
      }
    }
 
  },
})

使用:

<el-input type="textarea" rows="3" v-wordlimit="20" v-model="value"></el-input>

3、v-anthor

需求:点击某个元素(通常是标题、副标题之类的),动画滚动到对应的内容块

思路:

定时器使用window.scrollBy

不考虑ie的话,可直接使用 window.scrollBy({ top: ,left:0,behavior:'smooth' })

代码:

Vue.directive('anchor',{
  inserted(el,binding){
    let { value } = binding
    let timer = null
    el.addEventListener('click',function(){
      // 当前元素距离可视区域顶部的距离
      let currentTop = el.getBoundingClientRect().top
      animateScroll(currentTop)
    },false)
     
    function animateScroll(currentTop){
      if(timer){
        clearInterval(timer)
      }
      let c = 9
      timer = setInterval(() =>{
        if(c==0){
          clearInterval(timer)
        }
        c--
        window.scrollBy(0,(currentTop-value)/10)
      },16.7)
    }
 
  }
})

使用:

<div class="box" v-anchor="20" >是的</div>

4、v-hasRole

需求:根据系统角色添加或删除相应元素

代码:

Vue.directive('hasRole',{
  inserted(el,binding){
    let { value } = binding
    let roles = JSON.parse(sessionStorage.getItem('userInfo')).roleIds
 
    if(value && value instanceof Array && value.length>0){
 
      let hasPermission = value.includes(roles)
 
      if(!hasPermission){
        el.parentNode && el.parentNode.removeChild(el)
      }
    }else{
      throw new Error(`请检查指令绑定的表达式,正确格式例如 v-hasRole="['admin','reviewer']"`)
    }
  }
})

看完了这篇文章,相信你对“vue中自定义指令怎么用”有了一定的了解,如果想了解更多相关知识,欢迎关注行业资讯频道,感谢各位的阅读!

内容来源网络,如有侵权,联系删除,本文地址:https://www.230890.com/zhan/148251.html

(0)

相关推荐

  • C#操作Excel实现的实例分析

    技术C#操作Excel实现的实例分析C#操作Excel实现的实例分析,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。C#操作Excel是怎么样执行的呢?我们在

    攻略 2021年11月24日
  • nagios如何监控linux客户端主机

    技术nagios如何监控linux客户端主机这篇文章主要介绍nagios如何监控linux客户端主机,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!nagios监控linux主机1.客户端自定义监控

    攻略 2021年10月21日
  • 快速排序平均时间复杂度O(nlogn)的推导

    技术快速排序平均时间复杂度O(nlogn)的推导 快速排序平均时间复杂度O(nlogn)的推导快速排序作为随机算法的一种,不能通过常规方法来计算时间复杂度,本文记录了一种推导方法快速排序作为随机算法的一

    礼包 2021年11月13日
  • thinkphp怎么开启api(thinkphp命令行怎么设置)

    技术thinkphp大d方法怎么使用这篇文章主要介绍“thinkphp大d方法怎么使用”,在日常操作中,相信很多人在thinkphp大d方法怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家

    攻略 2021年12月16日
  • Bootstrap-table 账号维护

    技术Bootstrap-table 账号维护 Bootstrap-table 账号维护https://www.cnblogs.com/laowangc/p/8875526.html
    https://ww

    礼包 2021年11月11日
  • 山药鸡蛋饼的做法,山药的各种做法宝宝吃法

    技术山药鸡蛋饼的做法,山药的各种做法宝宝吃法【奶香山药小馒头】铁棍山药山药鸡蛋饼的做法,面粉,牛奶1.取适量山药上锅蒸熟,压成山药泥,晾凉至室温;
    2.山药泥中加2g酵母、100ml牛奶搅拌均匀;
    3.加200g面粉和成

    生活 2021年10月24日