vue2自定义指令

注册全局指令

Vue提供了一个directive方法给我们注册自定义指令,在main.js中注册一个全局的自定义指令。

import Vue from 'vue'
// 注册全局指令
Vue.directive("resize", {
  bind() {},
  inserted() {},
  update() {},
  componentUpdated() {},
  unbind() {},
});

new Vue({
  render: h => h(App)
}).$mount('#app')

// 使用
<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" v-resize />
  </div>
</template>

接收两个参数:指令名称、包含指令钩子函数的对象。

注册局部指令

Vue提供了一个directives方法给我们注册自定义指令,注册局部自定义指令

<template>
  <div id="app">
    <img alt="Vue logo" src="./assets/logo.png" v-resize />
  </div>
</template>

<script>
export default {
  directives: {
    resize: {
      bind() {
        console.log("hu初始化");
      },
      inserted() {
        console.log("元素被插入到父节点");
      },
      update() {},
      componentUpdated() {},
      unbind() {},
    },
  },
};
</script>

*注意点:directive是注册全局 directives是注册局部

批量注册指令

src目录下新建utils文件夹,然后建两个文件名字自取
1、tools.js 用于写自定义的指令

const resize = {
  bind() {
    console.log("resize初始化")
  },
  inserted() {
    console.log("元素被插入到父节点")
  },
  update() {},
  componentUpdated() {},
  unbind() {},
}

const copy = {
    bind() {
      console.log("copy初始化")
    },
    inserted() {
      console.log("元素被插入到父节点")
    },
    update() {},
    componentUpdated() {},
    unbind() {},
  }

export { resize, copy }

2、directives.js 用于批量注册指令

// 自定义指令
import * as directives from './tools'

// 批量注册指令
export default {
    install(Vue) {
        Object.keys(directives).forEach((key) => {
            Vue.directive(key, directives[key])
        })
    }
}

3、main.js 中引入

import Directives from './utils/directives'
Vue.use(Directives)

4、页面上使用

<img alt="Vue logo" src="./assets/logo.png" v-resize />
<img alt="Vue logo" src="./assets/logo.png" v-copy />

5、效果


效果
自定义指令生命周期

bind:
只调用一次,指令第一次绑定到元素时调用。在这里可以进行一次性的初始化设置。

inserted:
被绑定元素插入父节点时调用 (仅保证父节点存在,但不一定已被插入文档中)。

update:
所在组件的 VNode 更新时调用,但是可能发生在其子 VNode 更新之前。指令的值可能发生了改变,也可能没有。但是你可以通过比较更新前后的值来忽略不必要的模板更新

componentUpdated:
指令所在组件的 VNode及其子 VNode全部更新后调用。
unbind:
只调用一次,指令与元素解绑时调用。

钩子函数顺序:
指令绑定到元素时(bind)
元素插入时(inserted)
组件更新时(update)
组件更新后(componentUpdated)
指令与元素解绑时(unbind)

生命周期参数

el:
指令所绑定的元素,可以用来直接操作 DOM。

binding:
一个对象,包含以下属性:
name:指令名,不包括 v- 前缀。
value:指令的绑定值,例如:v-my-directive="1 + 1" 中,绑定值为 2。
oldValue:指令绑定的前一个值,仅在 update 和 componentUpdated 钩子中可用。无论值是否改变都可用。
expression:字符串形式的指令表达式。例如 v-my-directive="1 + 1" 中,表达式为 "1 + 1"。
arg:传给指令的参数,可选。例如 v-my-directive:foo 中,参数为 "foo"。
modifiers:一个包含修饰符的对象。例如:v-my-directive.foo.bar 中,修饰符对象为 { foo: true, bar: true }。

vnode:
Vue 编译生成的虚拟节点。

oldVnode:
上一个虚拟节点,仅在update和componentUpdated钩子中可用。

el和binding参数是我们使用得最频繁的

自定义指令传参和传值
<template>
    <div
      style="height: 300px; width: 80%; background: blue"
      v-resize:[args]="value"
    ></div>
</template>

<script>
export default {
  data() {
    return {
      args: "我是参数",
      value: "我是传的值",
    };
  },
  components: {},
  directives: {
    resize: {
      bind(el, binding) {
        console.log("bind", binding);
        console.log("值", binding.value);
        console.log("参数", binding.arg);
      },
    },
  },
};
</script>

v-resize:[args]="value"
1、:[args(写变量名)]
2、value是要传的值
3、expression和arg要区分开 expression是传字符串 arg是传变量

场景

复制、缩放、拖拽等都可以使用自定义指令。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容