国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > JavaScript > 正文

Vue.directive()的用法和實例詳解

2024-05-06 15:21:08
字體:
來源:轉載
供稿:網友

官網實例:

https://cn.vuejs.org/v2/api/#Vue-directive

https://cn.vuejs.org/v2/guide/custom-directive.html

指令定義函數提供了幾個鉤子函數(可選):

bind: 只調用一次,指令第一次綁定到元素時調用,用這個鉤子函數可以定義一個在綁定時執行一次的初始化動作。
inserted: 被綁定元素插入父節點時調用(父節點存在即可調用,不必存在于 document 中)。

update: 被綁定元素所在的模板更新時調用,而不論綁定值是否變化。通過比較更新前后的綁定值,可以忽略不必要的模板更新(詳細的鉤子函數參數見下)。

componentUpdated: 被綁定元素所在模板完成一次更新周期時調用。

unbind: 只調用一次, 指令與元素解綁時調用。

本人菜鳥型,看官網蒙圈,然后百度Vue.directive()的實例和用法,有的很高深,有的不健全,我貼上兩個簡單的demo,希望對看到的朋友有幫助:

1、官網給出的demo,刷新頁面input自動獲取焦點:

<div id="app"> <SPAN style="WHITE-SPACE: pre"> </SPAN><input type="text" v-focus/> </div> <div id="app"> <input type="text" v-focus/></div>// 注冊一個全局自定義指令 v-focus  Vue.directive('focus', {   // 當綁定元素插入到 DOM 中。    inserted: function (el,binding) {     <SPAN style="WHITE-SPACE: pre"> </SPAN>// 聚焦元素      <SPAN style="WHITE-SPACE: pre"> </SPAN>el.focus();   } }); new Vue({   el:'#app' }); // 注冊一個全局自定義指令 v-focusVue.directive('focus', {  // 當綁定元素插入到 DOM 中。  inserted: function (el,binding) {   // 聚焦元素   el.focus(); }});new Vue({  el:'#app'});

2、一個拖拽的demo: 1)被拖拽的元素必須用position定位,才能被拖動;

2)自定義指令完成后需要實例化Vue,掛載元素;

3)inserted: 被綁定元素插入父節點時調用(父節點存在即可調用,不必存在于 document 中)。

<style type="text/css">   .one,.two{     height:100px;     width:100px;     border:1px solid #000;     position: absolute;     -webkit-user-select: none;     -ms-user-select: none;     -moz-user-select: -moz-none;     cursor: pointer;   }   .two{     left:200px;   } </style> <div id="app">   <div class="one" v-drag>拖拽one</div>   <div class="two" v-drag>拖拽two</div> </div> <style type="text/css"> .one,.two{ height:100px; width:100px; border:1px solid #000; position: absolute; -webkit-user-select: none; -ms-user-select: none; -moz-user-select: -moz-none; cursor: pointer; } .two{ left:200px; }</style><div id="app"> <div class="one" v-drag>拖拽one</div> <div class="two" v-drag>拖拽two</div></div>[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' });             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 拉萨市| 华宁县| 闵行区| 苗栗市| 洛阳市| 龙南县| 化隆| 石首市| 芷江| 永春县| 新竹市| 拜城县| 拉孜县| 双流县| 沾化县| 繁昌县| 霍州市| 永和县| 凌云县| 台南县| 长沙市| 徐水县| 封丘县| 黔东| 新宁县| 远安县| 张家界市| 喜德县| 山丹县| 冀州市| 哈尔滨市| 浦北县| 娄底市| 永川市| 嵩明县| 民权县| 如东县| 加查县| 鄯善县| 高碑店市| 道孚县|