v-focus(全局自定义指令钩子)
<div id="box"> {{msg}} <p v-text="msg"></p> <p v-html="msg"></p> <input id="input" type="text" v-focus> </div> <!--引入vue.js--> <script src="./base/vue.js"></script> <script> //要操作底层dom元素,例如初始化的时候让输入框获取焦点 //全局自定义指令 局部自定义指令 //内置指令 v-if v-show v-else v-for v-once v-model v-on v-bind v-text v-html //全局注册自定义指令钩子:v-focus /* Vue.directive("focus",{ //当被绑定元素插入到父节点的时候,就会触发inserted钩子函数 inserted(el){ el.focus() //聚焦元素 } }) */ new Vue({ el:"#box", directives:{ focus:{ inserted(el){ el.focus() //聚焦元素 } } }, data:{ msg:"<h2>我是h2</h2>" } }) </script>: 一个指令定义对象可以提供如下几个钩子函数 (均为可选):
bind:只调用一次,指令第一次绑定到元素时调用。在这里可以进行一次性的初始化设置。
inserted:被绑定元素插入父节点时调用 (仅保证父节点存在,但不一定已被插入文档中)。
update:所在组件的 VNode 更新时调用,但是可能发生在其子 VNode 更新之前。指令的值可能发生了改变,也可能没有。但是你可以通过比较更新前后的值来忽略不必要的模板更新 (详细的钩子函数参数见下)。
我们会在稍后讨论渲染函数时介绍更多 VNodes 的细节。
componentUpdated:指令所在组件的 VNode 及其子 VNode 全部更新后调用。
unbind:只调用一次,指令与元素解绑时调用。
<body> <div id="app"> <div id="app"> <my-comp v-if="msg" :msg="msg"></my-comp> <button @click="update">更新</button> <button @click="uninstall">卸载</button> <button @click="install">安装</button> </div> </div> <!--引入vue.js--> <script src="./base/vue.js"></script> <script> //创建了一个全局的自定义指令 v-hello Vue.directive('hello', { bind: function (el){ console.log('bind'); }, inserted: function (el){ console.log('inserted'); }, update: function (el){ console.log('update'); }, componentUpdated: function (el){ console.log('componentUpdated'); }, unbind: function (el){ console.log('unbind'); } }); new Vue({ el: '#app', data: { msg: 'Hello' }, components: { myComp: { template: '<h1 v-hello>{{msg}}</h1>', props: { msg: String } } }, methods: { update: function (){ this.msg = 'Hi'; }, uninstall: function (){ this.msg = ''; }, install: function (){ this.msg = 'Hello'; } } }) </script>指令钩子函数会被传入以下参数:
el:指令所绑定的元素,可以用来直接操作 DOM。 binding:一个对象,包含以下 property: name:指令名,不包括 v- 前缀。 value:指令的绑定值,例如:v-my-directive=“1 + 1” 中,绑定值为 2。 oldValue:指令绑定的前一个值,仅在 update 和 componentUpdated 钩子中可用。无论值是否改变都可用。 expression:字符串形式的指令表达式。例如 v-my-directive=“1 + 1” 中,表达式为 “1 + 1”。 arg:传给指令的参数,可选。例如 v-my-directive:foo 中,参数为 “foo”。 modifiers:一个包含修饰符的对象。例如:v-my-directive.foo.bar 中,修饰符对象为 { foo: true, bar: true }。 vnode:Vue 编译生成的虚拟节点。移步 VNode API 来了解更多详情。 oldVnode:上一个虚拟节点,仅在 update 和 componentUpdated 钩子中可用。
这是一个使用了这些 property 的自定义钩子样例:
<div id="hook-arguments-example" v-demo:foo.a.b="message"></div> 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: '#hook-arguments-example', data: { message: 'hello!' } })