Vue 指令库

807 阅读2分钟

v-hotkey

这个指令可以给组件绑定一个或多个快捷键

<template>
 <div
  v-show="show"
  v-hotkey="{
   'esc': onClose,
   'ctrl+enter': onShow
  }"
 >
   Press `esc` to close me!
 </div>
</template>
 
<script>
export default {
  data() {
    return {
      show: true
    }
  },
 
  methods: {
    onClose() {
      this.show = false
    },
 
    onShow() {
      this.show = true
    },
  }
}
</script>

v-click-outside

点击外部区域关掉某个组件,用这个指令可以轻松实现

<template>
 <div
  v-show="show"
  v-click-outside="onClickOutside"
 >
  Hide me when a click outside this element happens
 </div>
</template>

HTML

<script>
export default {
 data() {
  return {
   show: true
  };
 },

 methods: {
  onClickOutside() {
   this.show = false;
  }
 }
};
</script>

v-clipboard

可以用在任何静态或动态元素上。当元素被点击时,指令的值会被复制到剪贴板上

<button v-clipboard="value">
 Copy to clipboard
</button>

vue-scrollto

指令监听元素的点击事件,然后滚动到指定位置。我通常用来处理文章目录跳转和导航跳转

<span v-scroll-to="{
 el: '#element',     // 滚动的目标位置元素
 container: '#container', // 可滚动的容器元素
 duration: 500,      // 滚动动效持续时长(毫秒)
 easing: 'linear'     // 动画曲线
 }"
>
 Scroll to #element by clicking here
</span>

vue-lazyload

图片懒加载,非常方便

<img v-lazy="https://www.domain.com/image.jpg">

v-tooltip

指令可以给元素添加响应式的tooltip,并可控制显示位置、触发方式和监听事件

<button v-tooltip="'You have ' + count + ' new messages.'">

v-scroll-lock

基于 body-scroll-lock 开发,这个指令的作用是在打开模态浮层的时候防止下层的元素滚动

<template>
 <div class="modal" v-if="opened">
  <button @click="onCloseModal">X</button>
  <div class="modal-content" v-scroll-lock="opened">
   <p>A bunch of scrollable modal content</p>
  </div>
 </div>
</template>

<script>
export default {
 data () {
  return {
   opened: false
  }
 },
 methods: {
  onOpenModal () {
   this.opened = true
  },
 
  onCloseModal () {
   this.opened = false
  }
 }
}
</script>

v-money

如果你需要在输入框里加上货币前缀或后缀、保留小数点位数或者设置小数点符号

<template>
 <div>
  <input v-model.lazy="price" v-money="money" /> {{price}}
 </div>
</template>


<script>
export default {
 data () {
  return {
   price: 123.45,
   money: {
    decimal: ',',
    thousands: '.',
    prefix: '$ ',
    precision: 2,
   }
  }
 }
}
</script>

vue-infinite-scroll

无限滚动指令,当滚动到页面底部时会触发绑定的方法

<template>
 <!-- ... -->
 <div
  v-infinite-scroll="onLoadMore"
  infinite-scroll-disabled="busy"
  infinite-scroll-distance="10"
 ></div>
<template>

<script>
export default {
 data() {
  return {
   data [],
   busy: false,
   count: 0
  }
 },
 
 methods: {
  onLoadMore() {
   this.busy = true;
 
   setTimeout(() => {
    for (var i = 0, j = 10; i < j; i++) {
     this.data.push({ name: this.count++ });
    }
    this.busy = false;
   }, 1000);
  }
 }
}
</script>

@clampy-js/vue-clampy

这个指令会截断元素里的文本,并在末尾加上省略号。它是用clampy.js实现的

<p v-clampy="3">Long text to clamp here</p>

vue-inputmask

当你需要在输入框里格式化日期时,这个指令会自动生成格式化文本。基于Inputmask library 开发

<input type="text" v-mask="'99/99/9999'" />

vue-ripple-directive

这个指令可以给点击的元素添加波纹动效

<div v-ripple class="button is-primary">This is a button</div>

vue-focus

用户在界面里操作,让某个输入框获得焦点

<template>
 <button @click="focused = true">Focus the input</button>
 
 <input type="text" v-focus="focused">
</template>

<script>
export default {
 data: function() {
  return {
   focused: false,
  };
 },
};
</script>

v-blur

假设你的页面在访客没有注册的时候,有些部分需要加上半透明遮罩。用这个指令可以轻松实现,还可以自定义透明度和过渡效果

<template>
 <button
  @click="blurConfig.isBlurred = !blurConfig.isBlurred"
 >Toggle the content visibility</button>
 
 <p v-blur="blurConfig">Blurred content</p>
</template>

<script>
 export default {
   data () {
    return     
     blurConfig: {
      isBlurred: false,
      opacity: 0.3,
      filter: 'blur(1.2px)',
      transition: 'all .3s linear'
     }
    }
   }
  }
 };
</script>

vue-dummy

开发 app 的时候,偶尔会需要使用假文本数据,或者特定尺寸的占位图片。用这个指令可以轻松实现

<template>
 <!-- the content inside will have 150 words -->
 <p v-dummy="150"></p>
 
 <!-- Display a placeholder image of 400x300-->
 <img v-dummy="'400x300'" />
</template>