目录
本文主要介绍如何在Vue.js如何自定义全局和局部指令,以两个案例进行介绍,分别自定义v-focus和v-color指令。v-focus实现在表单输入框获取焦点,v-color可设置元素的背景颜色。
1.自定义全局指令
基本语法如下:Vue.directive(指令名,function(绑定的元素,......){
})
Vue.directive('focus', {
inserted: function(element) {
element.focus()
}
})
Vue.directive('color', {
bind: function(e, b) {
e.style.backgroundColor = b.value
}
})
定义完即可在元素上使用,需要加上“v-”前缀
<div id="app">
<div><input type="text" name="" id=""></div>
<div><input type="text" name="" id="" v-focus></div>
<div><input type="text" name="" id="" v-color="color"></div>
</div>
完整代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<div><input type="text" name="" id=""></div>
<div><input type="text" name="" id="" v-focus></div>
<div><input type="text" name="" id="" v-color="color"></div>
</div>
<script src="./vue.min.js"></script>
<script>
Vue.directive('focus', {
inserted: function(element) {
element.focus()
}
})
Vue.directive('color', {
bind: function(e, b) {
e.style.backgroundColor = b.value
}
})
new Vue({
el: '#app',
data: {
color: '#674522'
},
methods: {
}
})
</script>
</body>
</html>
2.自定义局部指令
在directives属性中添加局部指令
new Vue({
el: '#app',
data: {
color: '#674522'
},
methods: {},
directives: {
focus: {
inserted: function(element) {
element.focus()
}
},
color: {
bind: function(e, b) {
e.style.backgroundColor = b.value
}
}
}
})
使用方法同全局指令一样,在相应元素中添加自定义的指令即可
<div id="app">
<div><input type="text" name="" id=""></div>
<div><input type="text" name="" id="" v-focus></div>
<div><input type="text" name="" id="" v-color="color"></div>
</div>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="app">
<div><input type="text" name="" id=""></div>
<div><input type="text" name="" id="" v-focus></div>
<div><input type="text" name="" id="" v-color="color"></div>
</div>
<script src="./vue.min.js"></script>
<script>
new Vue({
el: '#app',
data: {
color: '#674522'
},
methods: {},
directives: {
focus: {
inserted: function(element) {
element.focus()
}
},
color: {
bind: function(e, b) {
e.style.backgroundColor = b.value
}
}
}
})
</script>
</body>
</html>
效果如图:
第二个input文本框会自动获取焦点,第三个input文本框的背景会变色
3.自定义指令的不同函数
(1)只写function()时,会在绑定数据及更新时调用
(2)可写三个函数,bind绑定时调用,inserted插入时调用,update更新时调用。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="./vue.min.js"></script>
</head>
<body>
<div id="app">
<div><input type="text" v-big="num"></div>
<div><input type="text" v-small="num"></div>
<div><button @click="add">点击</button></div>
</div>
<script>
//绑定和更新时调用
Vue.directive('small', function(e, b) {
e.value = b.value - 100
})
new Vue({
el: '#app',
data() {
return {
color: 'skyblue',
size: '100px',
num: 100
}
},
methods: {
add() {
this.num++
}
},
directives: {
big: {
// 绑定数据时
bind(e, b) {
e.value = b.value
},
// 插入元素时
inserted(e, b) {
e.focus()
},
// 更新时
update(e, b) {
e.value = b.value * 100
}
}
}
})
</script>
</body>
</html>