这篇文章主要介绍了Vue中的自定义指令的相关资料,帮助大家更好的理解和学习vue框架,感兴趣的朋友可以了解下
除了默认设置的核心指令( v-model 和 v-show ),Vue 也允许注册自定义指令。在Vue里,代码复用的主要形式和抽象是组件。然而,有的情况下,仍然需要对纯 DOM 元素进行底层操作,这时候就会用到自定义指令。本文将详细介绍Vue自定义指令
指令注册
以一个input元素自动获得焦点为例,当页面加载时,使用autofocus可以让元素将获得焦点 。但是autofocus在移动版Safari上不工作。现在注册一个使元素自动获取焦点的指令
指令注册类似于组件注册,包括全局指令和局部指令两种
【全局指令】
使用Vue.diretive()来全局注册指令
1 2 3 4 |
|
【局部指令】
也可以注册局部指令,组件或Vue构造函数中接受一个 directives 的选项
1 2 3 4 5 6 7 8 9 10 |
|
然后可以在模板中任何元素上使用新的 v-focus 属性
1 2 |
|
1 2 3 4 5 |
|
钩子函数
指令定义函数提供了几个钩子函数(可选)
【bind】
只调用一次,指令第一次绑定到元素时调用,用这个钩子函数可以定义一个在绑定时执行一次的初始化动作
【inserted】
被绑定元素插入父节点时调用(父节点存在即可调用,不必存在于 document 中)
【update】
所在组件的 VNode 更新时调用,但是可能发生在其孩子的 VNode 更新之前。指令的值可能发生了改变也可能没有。但是可以通过比较更新前后的值来忽略不必要的模板更新
【componentUpdated】
所在组件的 VNode 及其孩子的 VNode 全部更新时调用
【unbind】
只调用一次, 指令与元素解绑时调用
钩子函数参数
钩子函数被赋予了以下参数
【el】
指令所绑定的元素,可以用来直接操作 DOM
【binding】
一个对象,包含以下属性:
1 2 3 4 5 6 |
|
【vnode】
Vue 编译生成的虚拟节点
【oldVnode】
上一个虚拟节点,仅在 update 和 componentUpdated 钩子中可用
[注意]除了 el 之外,其它参数都是只读的,尽量不要修改他们。如果需要在钩子之间共享数据,建议通过元素的 dataset 来进行
下面是一个使用了这些参数的自定义钩子样例
1 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
【函数简写】
大多数情况下,可能想在bind和update钩子上做重复动作,并且不想关心其它的钩子函数。可以这样写:
1 2 3 |
|
【对象字面量】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