[Development] vue vue tutorial instruction Vue.directive

1. Registration instructions

With assembly instructions need to register to use the same, the same, there are two ways, one is global registration:

?
1
2
3
4
5
Vue.directive( 'dirName' , function (){
 
   //定义指令
 
});

Another is a partial registration:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
new Vue({
 
  directives:{
 
    dirName:{
 
       //定义指令
 
    }
 
  }
 
});

2. Definition instruction

Instruction definition, the official five hook functions for our use, representing the various components of the life cycle of a

bind: called only once, the first instruction when the call is bound to the element, the hook function can be defined by a first initialization operation performed at the time of binding.

inserted: calls (calls to a parent node is present, need not be present in the document) is inserted into the parent binding element.

update: Called when the template is updated binding element is located, regardless of whether the binding value changes. By comparing the binding values ​​before and after the update, you can ignore unnecessary template update (hook function parameters detailed below).

componentUpdated: template element is bound where the call is completed when the next update cycle.

unbind: is called only once, when the call instruction and tie element solution.

Here are a few other good understanding about the template update (update) here, my understanding is: The instruction templates where changes need to re-render time, such as when an input box model changes will trigger command . Of course, here to say vague, specific yet to be studied.

This code can be achieved using the update on

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
< div id = "app" >
  < input type = "text" v-focus = "name" v-model = "name" :data-name = "name" >
  < button type = "button" @ click = "name='zw'" >click</ button >
 
   <!--当点击按钮的时候name改变,继而触发update中的方法-->
</ div >
< script >
 
  Vue.directive('focus',{
    bind: function(el, binding){
    console.log('bind:',binding.value);
  },
  inserted: function(el, binding){
    console.log('insert:',binding.value);
  },
  update: function(el, binding, vnode, oldVnode){
    el.focus();
    console.log(el.dataset.name);//这里的数据是可以动态绑定的
    console.table({
      name:binding.name,
      value:binding.value,
      oldValue:binding.oldValue,
      expression:binding.expression,
      arg:binding.arg,
      modifiers:binding.modifiers,
      vnode:vnode,
      oldVnode:oldVnode
    });
  },
  componentUpdated: function(el, binding){
    console.log('componentUpdated:',binding.name);
  }
});
new Vue({
  el:'#app',
  data:{
    name:'zwzhai'
  }
});
</ script >

3. Definition of the hook function

Here are a few parameters provided by the official:

el: 指令所绑定的元素,可以用来直接操作 DOM 。
binding: 一个对象,包含以下属性:
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 编译生成的虚拟节点,查阅 VNode API 了解更多详情。
oldVnode: 上一个虚拟节点,仅在 update 和 componentUpdated 钩子中可用。

这几个参数,大家看文档也能理解,就不多说了,关于bingding的几个属性说一下自己的看法,value这个属性,可以传字面量,也可以传单条语句(如上),还可以以变量的形式如<input type="text" v-focus="name" v-model="name">;arg这里可以传一个字符串,因为在value去访问绑定值得时候拿到的不是直接写的那个,也就是说v-focus="name",这个name永远都是一个变量,需要定义赋值,而arg可以直接访问该值,如v-focus:foo,那么在钩子函数中拿到的就是foo这个字符串。

在vue的指令中是不可以双向数据绑定的,如官方所说:除了 el 之外,其它参数都应该是只读的,尽量不要修改他们。如果需要在钩子之间共享数据,建议通过元素的 dataset 来进行。这里补充下关于dataset的知识:

data-  是html5的一个新属性,查了下浏览器支持程度,目前的主流浏览器都是支持的,IE的话要到10以上。具体的使用:在HTML中以属性的方式去写,data-yourname="value",在js中取这个属性就不用使用getAttribute这个方法,而是直接访问,ele.dataset.yourname,在js中你也可以添加和删除,添加:ele.dataset.dessert="yourname",删除:dette ele.dataset.name。此外,这个属性可以用作css选择器:.class[data-name]:{}。

使用示例:

组件中使用方法:

app.vue中注册:

全局主题变量:

Guess you like

Origin www.cnblogs.com/xiaohuizhang/p/11589800.html