美文网首页
vue 局部指令和全局指令

vue 局部指令和全局指令

作者: 暴躁程序员 | 来源:发表于2022-03-25 10:08 被阅读0次

vue 自定义指令常用于 DOM 操作

局部指令

1. 简单使用

在组件中

<template>
  <div>
    <label for="a" v-test></label>
    <input id="a" v-focus type="text" />
  </div>
</template>
<script>
export default {
  directives: {
    focus: {
      inserted: function (el) {
        el.focus();
      },
    },
    test: {
      inserted: function (el) {
        el.innerHTML = "测试";
      },
    },
  },
};
</script>
2. 封装
  1. 新建 src/utils/localDirectives.js,创建指令对象
// 按需引入

export const focus = {
  inserted: function(el) {
    el.focus();
  }
};

export const test = {
  inserted: function(el) {
    el.innerHTML = "测试";
  }
};

  1. 在组件中,引入并使用
<template>
  <div>
    <label for="a" v-test></label>
    <input id="a" v-focus type="text" />
  </div>
</template>
<script>
import { focus, test } from "./utils/localDirectives";
export default {
  directives: { focus, test },
};
</script>

全局指令

1. 简单使用
  1. 在组件中使用
<template>
  <div>
    <label for="a" v-test></label>
    <input id="a" v-focus type="text" />
  </div>
</template>
<script>
export default {};
</script>
  1. 在 main.js 中注册
Vue.directive("focus", {
  inserted: function(el) {
    el.focus();
  }
});

Vue.directive("test", {
  inserted: function(el) {
    el.innerHTML = "测试";
  }
});
2. 封装
  1. 新建 src/utils/globalDirectives.js,创建指令对象
// 全部引入

const focus = {
  inserted: function(el) {
    el.focus();
  }
};

const test = {
  inserted: function(el) {
    el.innerHTML = "测试";
  }
};

export default {
  focus,
  test
};
  1. 新建 src/utils/global.js,在 install 函数中,遍历重构指令对象
// 引入指令
import directives from "./globalDirectives.js";

const globals = {
  install: function(Vue, option) {
    Object.keys(directives).forEach(key => {
      // Object.keys(directives) //返回数组,数组里面的每一项是对象的属性名称
      Vue.directive(key, directives[key]);
    });
  }
};

export default globals;

  1. 在 main.js 中,引入并 use
import globals from "@/utils/global.js";
Vue.use(globals);
  1. 在组件中使用
<template>
  <div>
    <label for="a" v-test></label>
    <input id="a" v-focus type="text" />
  </div>
</template>
<script>
export default {};
</script>

自定义指令的五个钩子函数和钩子函数的三个参数

注意:指令钩子函数的 this 是 undefined

自定义指令的五个钩子函数

bind: 初始化只调用一次(dom 未创建就调用)
inserted: 初始化调用(dom 创建之后调用)
update: 组件更新前调用
componentUpdated: 组件更新后调用。
unbind: 只调用一次, 指令与元素解绑时调用。

export default {
  directives: {
    focus: {
      bind() {
        console.log("bind");
      },
      inserted(el) {
        el.focus();
        console.log("inserted");
      },
      update() {
        console.log("update");
      },
      componentUpdated() {
        console.log("componentUpdated");
      },
      unbind() {
        console.log("unbind");
      },
    },
  },
};
钩子函数的四个参数
  1. 常用于操作 dom
    el: 指令所绑定的 DOM 元素
  2. 常用于获取指令参数
    binding: {
    name: 指令名称,不包括 v- 前缀。
    value: 指令的绑定值, 例如: v-foucus="1 + 1", value 的值是 2。
    oldValue: 指令绑定的前一个值,仅在 update 和 componentUpdated 钩子中可用。无论值是否改变都可用。
    expression: 绑定值的表达式或变量名。 例如 v-my-directive="1 + 1" , expression 的值是 "1 + 1"。
    arg: 传给指令的参数。例如 v-my-directive:foo, arg 的值是 "foo"。
    modifiers: 一个包含修饰符的对象。 例如: v-my-directive.foo.bar, 修饰符对象 modifiers 的值是 { foo: true, bar: true }。
    }
  3. vnode: Vue 编译生成的虚拟节点。
  4. oldVnode: 上一个虚拟节点,仅在 update 和 componentUpdated 钩子中可用。

相关文章

  • 40.Vue自定义指令--局部

    Vue指令详解参考 当全局指令和局部指令同名时以局部指令为准 案例(局部指令聚焦输入框): index.vue

  • Vue之自定义指令

    分类:全局指令、局部指令 1、自定义全局指令 使用全局方法Vue.directive(指令ID,定义对象) 注:使...

  • Vue基础(五)--自定义指令与过渡

    1.自定义指令 分类:全局指令、局部指令 1.1 自定义全局指令 使用全局方法 Vue.directive(指令I...

  • vue 局部指令和全局指令

    vue 自定义指令常用于 DOM 操作 局部指令 1. 简单使用 在组件中 2. 封装 新建 src/utils/...

  • vue 有自定义指令

    vue 的自定义指令,分为全局自定义指令和局部自定义指令,局部自定义指令等价于局部组件。 自定义指令可以对DOM进...

  • vue2中的directives

    directives: 用于自定义vue指令 注册全局自定义指令:调directive(id, fun) 注册局部...

  • 8 个非常实用的 Vue 自定义指令

    Vue 自定义指令有全局注册和局部注册两种方式。先来看看注册全局指令的方式,通过 Vue.directive( i...

  • Vue自定义指令

    Vue 自定义指令 全局定义 局部定义 Vue 指令生命周期钩子 bind:只调用一次,指令第一次绑定到元素时调用...

  • Vue 自定义指令

    Vue自定义指令 定义 通过directive方法,配合钩子函数及参数定义指令 定义全局自定义指令 定义局部自定义...

  • vue中自定义组件、指令、插件

    组件 全局组件 局部组件 自定义插件 提供install方法,挂载到Vue 指令 使用指令实现input自动获取焦...

网友评论

      本文标题:vue 局部指令和全局指令

      本文链接:https://www.haomeiwen.com/subject/dlkljrtx.html