美文网首页
vue自定义指令

vue自定义指令

作者: 呔你这妖精_9337 | 来源:发表于2022-11-08 15:42 被阅读0次
<template>
  <div class="home">
    <button @click="n++">n++</button>
    <div v-bignumber="n" style="width:100px;height:200px;border:1px solid #000;float: left;"></div>
    <input type="text" v-fbind:value="n">
     
  </div>
</template>

<script>
export default {
  name: 'HomeView',
  components: {},
  data() {
    return {
      n:1
    }
  },
  directives: {
//函数形式
    bignumber(element,binding){
      element.innerHTML = binding.value * 10
    },
//对象形式
    fbind:{//简写形式会触发bind和update,不会触发inserted
      //指令与元素绑定时触发
      bind(element,binding){
        element.value = binding.value
      },
      //指令所在元素被插入页面时触发
      inserted(element,binding){
        element.focus()
      },
      //指令所在的模板被重新解析时
      update(element,binding){
        element.focus()
      }
    }
  }
}
</script>

1、自定义指令有两种写法,函数形式(简写)和对象形式(完整形式)。简写形式只在两个时机触发:1、指令与元素成功绑定时(元素没有被插入页面),2、指令所在的模板被重新解析时;
2、指令名称不可以使用camelCase命名,多个单词的指令名称使用kebab-case(定义时需要加引号)
3、指令的回调函数中this不指向vue
4、自定义指令使用时加v-,定义时不需要加v-;
5、定义全局指令

vue.directive('fbind',{//简写形式会触发bind和update,不会触发inserted
      //指令与元素绑定时触发
      bind(element,binding){
        element.value = binding.value
      },
      //指令所在元素被插入页面时触发
      inserted(element,binding){
        element.focus()
      },
      //指令所在的模板被重新解析时
      update(element,binding){
        element.focus()
      }
    })

相关文章

  • season2-全局API

    第1节:Vue.directive 自定义指令 Vue.directive自定义指令 自定义的指令:changec...

  • Vue div节点滚动事件-加载更多

    使用Vue.directive注册全局指令 绑定事件 对于Vue自定义指令不明白的同学请移步: Vue自定义指令 ...

  • VUE-2:自定义指令、事件

    directive自定义指令 我们还可以通过`Vue`提供的directive方法来自定义指令 注册指令 `vue...

  • vue入门6---vue基本指令、自定义指令、插件

    一、vue常用指令概览 二、vue自定义指令 注册全局指令Vue.directive('my-directive'...

  • vue自定义指令初探

    vue自定义指令初探 一、什么是自定义指令 自定义指令是用来操作DOM的。尽管Vue推崇数据驱动视图的理念,但并非...

  • vue 有自定义指令

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

  • Vue指令钩子函数

    Vue指令上篇文章有讲过了,现在分析Vue指令钩子函数。 自定义指令 全局定义:Vue.directive( ' ...

  • vue自定义指令

    除了内置的指令外,Vue 也允许注册自定义指令。 vue用Vue.directive(id,definition)...

  • vue知识集锦(三)

    自定义指令 除了核心功能默认内置的指令 (v-model和v-show),Vue 也允许注册自定义指令。尽管Vue...

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

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

网友评论

      本文标题:vue自定义指令

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