Home  >  Article  >  Web Front-end  >  Detailed explanation on the usage of Vue.directive()

Detailed explanation on the usage of Vue.directive()

小云云
小云云Original
2018-03-05 09:47:311659browse

The instruction definition function provides several hook functions (optional):

bind: only called once, called when the instruction is bound to an element for the first time. This hook function can be used to define a An initialization action performed once during binding.
inserted: Called when the bound element is inserted into the parent node (it can be called as long as the parent node exists and does not have to exist in the document).

update: Called when the template where the bound element is located is updated, regardless of whether the binding value changes. By comparing the binding values ​​before and after the update, unnecessary template updates can be ignored (see below for detailed hook function parameters).

componentUpdated: Called when the template where the bound element is located completes an update cycle.

unbind: Called only once, when the instruction is unbound from the element.

I am a novice. I looked at the official website and was confused, and then Baidu Vue.directive() examples and usage, some are very profound, some are not perfect, I posted two simple ones Demo, I hope it will be helpful to friends who see it:

1. The demo given by the official website, refresh the page input and automatically gain focus:


// 注册一个全局自定义指令 v-focus Vue.directive('focus', { // 当绑定元素插入到 DOM 中。 inserted: function (el,binding) { // 聚焦元素 el.focus(); } }); new Vue({   el:'#app' }); // 注册一个全局自定义指令 v-focus Vue.directive('focus', { // 当绑定元素插入到 DOM 中。 inserted: function (el,binding) { // 聚焦元素 el.focus(); } }); new Vue({   el:'#app' });

2. A drag-and-drop demo: 1) The dragged element must be positioned with position before it can be dragged;

2) After the custom instruction is completed, Vue needs to be instantiated and the element mounted;

3)inserted: Called when the bound element is inserted into the parent node (it can be called as long as the parent node exists, it does not have to exist in the document).


 

拖拽one

拖拽two

拖拽one

拖拽two

[javascript] view plain copy print?Vue.directive('drag', { inserted:function(el){ el.onmousedown=function(e){ let l=e.clientX-el.offsetLeft; let t=e.clientY-el.offsetTop; document.onmousemove=function(e){ el.style.left=e.clientX-l+'px'; el.style.top=e.clientY-t+'px'; }; el.onmouseup=function(){ document.onmousemove=null; el.onmouseup=null; } } } }) new Vue({   el:'#app' });

The above is the detailed content of Detailed explanation on the usage of Vue.directive(). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn