directive-自定义指令(属于破坏性更新)
Vue中有v-if,v-for,v-bind,v-show,v-model 等等一系列方便快捷的指令 今天一起来了解一下vue里提供的自定义指令

1.Vue3指令的钩子函数

  • created 元素初始化的时候
  • beforeMount 指令绑定到元素后调用 只调用一次
  • mounted 元素插入父级dom调用
  • beforeUpdate 元素被更新之前调用
  • update 这个周期方法被移除 改用updated
  • beforeUnmount 在元素被移除前调用
  • unmounted 指令被移除后调用 只调用一次

Vue2 指令 bind inserted update componentUpdated unbind

2.在setup内定义局部指令

但这里有一个需要注意的限制:必须以 vNameOfDirective 的形式来命名本地自定义指令,以使得它们可以直接在模板中使用。

  1. <template>
  2. <button @click="show = !show">开关{{show}} ----- {{title}}</button>
  3. <Dialog v-move-directive="{background:'green',flag:show}"></Dialog>
  4. </template>
  1. const vMoveDirective: Directive = {
  2. created: () => {
  3. console.log("初始化====>");
  4. },
  5. beforeMount(...args: Array<any>) {
  6. // 在元素上做些操作
  7. console.log("初始化一次=======>");
  8. },
  9. mounted(el: any, dir: DirectiveBinding<Value>) {
  10. el.style.background = dir.value.background;
  11. console.log("初始化========>");
  12. },
  13. beforeUpdate() {
  14. console.log("更新之前");
  15. },
  16. updated() {
  17. console.log("更新结束");
  18. },
  19. beforeUnmount(...args: Array<any>) {
  20. console.log(args);
  21. console.log("======>卸载之前");
  22. },
  23. unmounted(...args: Array<any>) {
  24. console.log(args);
  25. console.log("======>卸载完成");
  26. },
  27. };

3.生命周期钩子参数详解

第一个 el 当前绑定的DOM 元素
第二个 binding

  • instance:使用指令的组件实例。
  • value:传递给指令的值。例如,在 v-my-directive=”1 + 1” 中,该值为 2。
  • oldValue:先前的值,仅在 beforeUpdate 和 updated 中可用。无论值是否有更改都可用。
  • arg:传递给指令的参数(如果有的话)。例如在 v-my-directive:foo 中,arg 为 “foo”。
  • modifiers:包含修饰符(如果有的话) 的对象。例如在 v-my-directive.foo.bar 中,修饰符对象为 {foo: true,bar: true}。
  • dir:一个对象,在注册指令时作为参数传递。例如,在以下指令中

image.png
第三个 当前元素的虚拟DOM 也就是Vnode
第四个 prevNode 上一个虚拟节点,仅在 beforeUpdate 和 updated 钩子中可用

4.函数简写

你可能想在 mounted 和 updated 时触发相同行为,而不关心其他的钩子函数。那么你可以通过将这个函数模式实现

  1. <template>
  2. <div>
  3. <input v-model="value" type="text" />
  4. <A v-move="{ background: value }"></A>
  5. </div>
  6. </template>
  7. <script setup lang='ts'>
  8. import A from './components/A.vue'
  9. import { ref, Directive, DirectiveBinding } from 'vue'
  10. let value = ref<string>('')
  11. type Dir = {
  12. background: string
  13. }
  14. const vMove: Directive = (el, binding: DirectiveBinding<Dir>) => {
  15. el.style.background = binding.value.background
  16. }
  17. </script>
  18. <style>
  19. </style>

案例自定义拖拽指令

  1. <template>
  2. <div v-move class="box">
  3. <div class="header"></div>
  4. <div>
  5. 内容
  6. </div>
  7. </div>
  8. </template>
  9. <script setup lang='ts'>
  10. import { Directive } from "vue";
  11. const vMove: Directive = {
  12. mounted(el: HTMLElement) {
  13. let moveEl = el.firstElementChild as HTMLElement;
  14. const mouseDown = (e: MouseEvent) => {
  15. //鼠标点击物体那一刻相对于物体左侧边框的距离=点击时的位置相对于浏览器最左边的距离-物体左边框相对于浏览器最左边的距离
  16. console.log(e.clientX, e.clientY, "-----起始", el.offsetLeft);
  17. let X = e.clientX - el.offsetLeft;
  18. let Y = e.clientY - el.offsetTop;
  19. const move = (e: MouseEvent) => {
  20. el.style.left = e.clientX - X + "px";
  21. el.style.top = e.clientY - Y + "px";
  22. console.log(e.clientX, e.clientY, "---改变");
  23. };
  24. document.addEventListener("mousemove", move);
  25. document.addEventListener("mouseup", () => {
  26. document.removeEventListener("mousemove", move);
  27. });
  28. };
  29. moveEl.addEventListener("mousedown", mouseDown);
  30. },
  31. };
  32. </script>
  33. <style lang='less'>
  34. .box {
  35. position: fixed;
  36. left: 50%;
  37. top: 50%;
  38. transform: translate(-50%, -50%);
  39. width: 200px;
  40. height: 200px;
  41. border: 1px solid #ccc;
  42. .header {
  43. height: 20px;
  44. background: black;
  45. cursor: move;
  46. }
  47. }
  48. </style>

————————————————
版权声明:本文为CSDN博主「小满zs」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq1195566313/article/details/123228132

————————————————
版权声明:本文为CSDN博主「小满zs」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq1195566313/article/details/123228132