详解Vue中的自定义指令

本文详述Vue中自定义指令的使用,包括全局和局部注册、钩子函数及其参数,例如bind、update等,以及如何通过指令实现特定功能,如元素自动获取焦点。通过实例展示了自定义指令在DOM操作中的作用。
摘要由CSDN通过智能技术生成

这篇文章主要介绍了Vue中的自定义指令的相关资料,帮助大家更好的理解和学习vue框架,感兴趣的朋友可以了解下

java

  除了默认设置的核心指令( v-model 和 v-show ),Vue 也允许注册自定义指令。在Vue里,代码复用的主要形式和抽象是组件。然而,有的情况下,仍然需要对纯 DOM 元素进行底层操作,这时候就会用到自定义指令。本文将详细介绍Vue自定义指令

指令注册
  以一个input元素自动获得焦点为例,当页面加载时,使用autofocus可以让元素将获得焦点 。但是autofocus在移动版Safari上不工作。现在注册一个使元素自动获取焦点的指令

  指令注册类似于组件注册,包括全局指令和局部指令两种

【全局指令】

  使用Vue.diretive()来全局注册指令

1

2

3

4

// 注册一个全局自定义指令 v-focusVue.directive('focus', { // 当绑定元素插入到 DOM 中。

 inserted: function (el) {  // 聚焦元素  el.focus()

 }

})

【局部指令】

  也可以注册局部指令,组件或Vue构造函数中接受一个 directives 的选项

1

2

3

4

5

6

7

8

9

10

var vm = new Vue({

 el: '#example',

 directives:{

  focus:{

   inserted: function (el) {

    el.focus()

   }  

  }

 }

})

  然后可以在模板中任何元素上使用新的 v-focus 属性

1

2

<p id="example">

 <input v-focus></p>

1

2

3

4

5

<script>// 注册一个全局自定义指令 v-focusVue.directive('focus', { // 当绑定元素插入到 DOM 中。 inserted: function (el) {  // 聚焦元素  el.focus()

 }

})var vm = new Vue({

 el: '#example',

})</script>

钩子函数
  指令定义函数提供了几个钩子函数(可选)

【bind】

  只调用一次,指令第一次绑定到元素时调用,用这个钩子函数可以定义一个在绑定时执行一次的初始化动作

【inserted】

  被绑定元素插入父节点时调用(父节点存在即可调用,不必存在于 document 中)

【update】

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

【componentUpdated】

  所在组件的 VNode 及其孩子的 VNode 全部更新时调用

【unbind】

  只调用一次, 指令与元素解绑时调用

钩子函数参数
  钩子函数被赋予了以下参数

【el】

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

【binding】

  一个对象,包含以下属性:

1

2

3

4

5

6

name: 指令名,不包括 v- 前缀。

value: 指令的绑定值, 例如: v-my-directive="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 }。

【vnode】

  Vue 编译生成的虚拟节点

【oldVnode】

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

  [注意]除了 el 之外,其它参数都是只读的,尽量不要修改他们。如果需要在钩子之间共享数据,建议通过元素的 dataset 来进行

  下面是一个使用了这些参数的自定义钩子样例

1

<p id="example" v-demo:foo.a.b="message"></p>

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

<script>Vue.directive('demo', {

 bind: function (el, binding, vnode) {  var s = JSON.stringify

  el.innerHTML =

   'name: '    + s(binding.name) + '<br>' +

   'value: '   + s(binding.value) + '<br>' +

   'expression: ' + s(binding.expression) + '<br>' +

   'argument: '  + s(binding.arg) + '<br>' +

   'modifiers: ' + s(binding.modifiers) + '<br>' +

   'vnode keys: ' + Object.keys(vnode).join(', ')

 }

})new Vue({

 el: '#example',

 data: {

  message: 'hello!'

 }

})</script>

【函数简写】

   大多数情况下,可能想在bind和update钩子上做重复动作,并且不想关心其它的钩子函数。可以这样写:

1

2

3

Vue.directive('color-swatch', function (el, binding) {

 el.style.backgroundColor = binding.value

})

【对象字面量】https://fontsup.com/profile/4o5ze14sjjk32-64.html
https://fontsup.com/profile/liked/4o5ze14sjjk32-64.html
https://fontsup.com/profile/sent/4o5ze14sjjk32-64.html
https://fontsup.com/profile/cqn767v5lrb26-12.html
https://fontsup.com/profile/liked/cqn767v5lrb26-12.html
https://fontsup.com/profile/sent/cqn767v5lrb26-12.html
https://fontsup.com/profile/eu9wci94cga62-15.html
https://fontsup.com/profile/liked/eu9wci94cga62-15.html
https://fontsup.com/profile/sent/eu9wci94cga62-15.html
https://fontsup.com/profile/nge5wx6xkej57-34.html
https://fontsup.com/profile/liked/nge5wx6xkej57-34.html
https://fontsup.com/profile/sent/nge5wx6xkej57-34.html
https://fontsup.com/profile/rzj86ba8uzc79-86.html
https://fontsup.com/profile/liked/rzj86ba8uzc79-86.html
https://fontsup.com/profile/sent/rzj86ba8uzc79-86.html
https://fontsup.com/profile/x1894gcyfyd45-15.html
https://fontsup.com/profile/liked/x1894gcyfyd45-15.html
https://fontsup.com/profile/sent/x1894gcyfyd45-15.html
https://fontsup.com/profile/qb2zubb8qno07-98.html
https://fontsup.com/profile/liked/qb2zubb8qno07-98.html
https://fontsup.com/profile/sent/qb2zubb8qno07-98.html
https://fontsup.com/profile/i0wgdvf7yqi07-61.html
https://fontsup.com/profile/liked/i0wgdvf7yqi07-61.html
https://fontsup.com/profile/sent/i0wgdvf7yqi07-61.html
https://fontsup.com/profile/9asb7klgsfc39-16.html
https://fontsup.com/profile/liked/9asb7klgsfc39-16.html
https://fontsup.com/profile/sent/9asb7klgsfc39-16.html
https://fontsup.com/profile/morju2kjgme71-60.html
https://fontsup.com/profile/liked/morju2kjgme71-60.html
https://fontsup.com/profile/sent/morju2kjgme71-60.html
https://fontsup.com/profile/gq65yojnset27-81.html
https://fontsup.com/profile/liked/gq65yojnset27-81.html
https://fontsup.com/profile/sent/gq65yojnset27-81.html
https://fontsup.com/profile/b6ji9keezfa38-23.html
https://fontsup.com/profile/liked/b6ji9keezfa38-23.html
https://fontsup.com/profile/sent/b6ji9keezfa38-23.html
https://fontsup.com/profile/gyg8k3p1trk82-69.html
https://fontsup.com/profile/liked/gyg8k3p1trk82-69.html
https://fontsup.com/profile/sent/gyg8k3p1trk82-69.html
https://fontsup.com/profile/1m36lkaonjf52-80.html
https://fontsup.com/profile/liked/1m36lkaonjf52-80.html
https://fontsup.com/profile/sent/1m36lkaonjf52-80.html
https://fontsup.com/profile/pmhccyuixxj80-32.html
https://fontsup.com/profile/liked/pmhccyuixxj80-32.html
https://fontsup.com/profile/sent/pmhccyuixxj80-32.html
https://fontsup.com/profile/s0abx92lows86-24.html
https://fontsup.com/profile/liked/s0abx92lows86-24.html
https://fontsup.com/profile/sent/s0abx92lows86-24.html
https://fontsup.com/profile/bjad49btlui98-36.html
https://fontsup.com/profile/liked/bjad49btlui98-36.html
https://fontsup.com/profile/sent/bjad49btlui98-36.html
https://fontsup.com/profile/ti1801k2fcc70-24.html
https://fontsup.com/profile/liked/ti1801k2fcc70-24.html
https://fontsup.com/profile/sent/ti1801k2fcc70-24.html
https://fontsup.com/profile/q5jd2lypezt97-59.html
https://fontsup.com/profile/liked/q5jd2lypezt97-59.html
https://fontsup.com/profile/sent/q5jd2lypezt97-59.html
https://fontsup.com/profile/xoyq1087qht11-19.html
https://fontsup.com/profile/liked/xoyq1087qht11-19.html
https://fontsup.com/profile/sent/xoyq1087qht11-19.html
https://fontsup.com/profile/qqkt7ck5fzg57-58.html
https://fontsup.com/profile/liked/qqkt7ck5fzg57-58.html
https://fontsup.com/profile/sent/qqkt7ck5fzg57-58.html
https://fontsup.com/profile/djpeh2mftbl67-3.html
https://fontsup.com/profile/liked/djpeh2mftbl67-3.html
https://fontsup.com/profile/sent/djpeh2mftbl67-3.html
https://fontsup.com/profile/6h8p91ppqhz68-31.html
https://fontsup.com/profile/liked/6h8p91ppqhz68-31.html
https://fontsup.com/profile/sent/6h8p91ppqhz68-31.html
https://fontsup.com/profile/oodugjbompm14-34.html
https://fontsup.com/profile/liked/oodugjbompm14-34.html
https://fontsup.com/profile/sent/oodugjbompm14-34.html
https://fontsup.com/profile/61odxeb4ddb57-33.html
https://fontsup.com/profile/liked/61odxeb4ddb57-33.html
https://fontsup.com/profile/sent/61odxeb4ddb57-33.html
https://fontsup.com/profile/j9edhmtgdm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值