vue-cli如何新增自定義指令?


vue-cli如何新增自定義指令?
1.創建局部指令

var app = new Vue({
el: '#app',
data: { 
},
// 創建指令(可以多個)
directives: {
// 指令名稱
dir1: {
inserted(el) {
// 指令中第一個參數是當前使用指令的DOM
console.log(el);
console.log(arguments);
// 對DOM進行操作
el.style.width = '200px';
el.style.height = '200px';
el.style.background = '#000';
}
}
}
})
2.全局指令

Vue.directive('dir2', {
inserted(el) {
console.log(el);
}
})
3.指令的使用

<div id="app">
<div v-dir1></div>
<div v-dir2></div>
</div>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM