首页 > 其他分享 >自定义指令directives

自定义指令directives

时间:2023-01-06 12:34:47浏览次数:35  
标签:console log 自定义 指令 directives inserted debugger

Vue 允许我们自定义指令,可以通过 Vue.directive 定义全局指令,可以在组件 directives 钩子中定义局部自定义指令

全局自定义指令

Vue.directive('input', {
  inserted: function(el, binding) {
    ...
  }
})

参数如下:

局部自定义

局部自定义指令定义在 directives 钩子中

<template>
  <div id="app">
    <div>{{ j }}</div>
    <input type="text" v-model="btnName">
    <button v-throttle="{time: 1000, func: printLog}">节流{{ btnName }}</button>
  </div>
</template>
<script>
export default {
  name: 'App',
  data(){
    return {
      j:{
        x:10
      },
      btnName: ""
    }
  },
  methods:{
    printLog(){
      console.log("打印")
    }
  },
  mounted(){
    this.$set(this.j, "z", 100)
  },
  directives:{
    throttle:{
      bind(){
        // 点击节流指令
        console.log("bind只会调用一次,指令第一次被绑定到元素上时触发");
        debugger
      },
      inserted(element, binding){
        console.log("节流指令");
        console.log("inserted是被绑定元素插入到父节点中时触发")
        let t = function(){
          let timer
          return function(){
            if(!timer){
              timer = setTimeout(()=>{
                timer = null
              }, binding.value.time || 1000)
              binding.value.func()
            }
          }
        }
        element.onclick = t()
        debugger
      },
      update(){
        console.log("vnode更新时触发");
        debugger
      },
      componentUpdated() {
        console.log('所在组件的VNode对象和其子类VNode对象全部更新后调用');
        debugger
      },
      unbind(){
        console.log('指令解绑时候被调用');
        debugger
      }
    }
  }
}
</script>

声明周期

自定义指令生命周期包括 bind,inserted,update,componentUpdated和unbind

如上述局部自定义指令所示:

 指令被绑定到元素上时,bind 执行,但此时元素不一定被插入到文档流中,如下:

绑定的元素插入父节点时触发 inserted

如图中蓝框所示,此时 mounted 中 Vue.set 还没有执行

在 input 框中输入后,执行 update,此时可以看到页面还没有重新渲染

 

如图蓝框,页面重新渲染后,执行 componentUpdated

 

 解绑时,触发 unbind 钩子

标签:console,log,自定义,指令,directives,inserted,debugger
From: https://www.cnblogs.com/xt112233/p/17030064.html

相关文章

  • 写过vue自定义指令吗,原理是什么?.m
    背景看了一些自定义指令的文章,但是探究其原理的文章却不多见,所以我决定水一篇。如何自定义指令?其实关于这个问题官方文档上已经有了很好的示例的,我们先来温故一下。除......
  • linux指令chown
    原文地址:https://blog.csdn.net/cnds123321/article/details/124957014chown 是 changeowner 的缩写语法该命令的语法如下:chown[-cfhvR][--help][--version]us......
  • Andorid自定义动画 文字与画布(一)
    Paint设置//设置画笔宽度paint.setStrokeWidth(5);//指定抗锯齿功能paint.setAntiAlias(true);//绘图样式paint.setStyle(Paint.Style.FILL);//设置文字对齐方式paint.setTe......
  • python-自定义模块及导入方法
    1.自定义模块​ 一个较大的程序一般应分为若干个程序块,若个程序块称为模块,每个模块用来实现一部分特定的功能。​ 这样做的目的是为了将代码有组织的存放在一起,方便管......
  • 项目测试中自定义时间
    在项目中测试有时候会加速时间,如活动是七天的周期,测试肯定得加速这个工具类就是为了这种情况,设定多少分一天就可以达到。useCustomDateTime在上线就改为false:使用Loca......
  • Linux连接终端时添加自定义提示信息
    vim./bashrcecho-e"\033{\033[5m"echo-e"\033[44;37m*********************************************************\033[5m"echo-e"\033[44;37m*/    ......
  • 自定义线程池
    自定义线程池packagecom.intell.config;​importorg.springframework.beans.factory.annotation.Value;importorg.springframework.context.annotation.Bean;importor......
  • 自定义注解+面向切面 日志记录
    自定义注解packagecom.example.spring.controller;​importjava.lang.annotation.*;​@Target(value=ElementType.METHOD)@Retention(RetentionPolicy.RUNTIME)@Docum......
  • 用XML自定义Excel功能区
    一、XML编写<?xmlversion="1.0"encoding="utf-8"?><customUIxmlns="http://schemas.microsoft.com/office/2006/01/customui"><ribbonstartFromScratch="fal......
  • C# 处理实体类赋值(获取嵌套类型,支持list 自定义类型)
    publicstaticTAESEncrypt<T>(Tobj)whereT:class{if(obj==null){returnobj;}varproperties=typeof(T).GetProperties();foreach(System.Reflecti......