自定义指令
2022/9/7
自定义指令
定义方法
局部指令
new Vue({
directives:{指令名:配置对象}
})new Vue({
directives:{指令名:回调函数}
})全局指令
Vue.directive(指令名,配置对象)Vue.directive(指令名,回调函数)
Vue.directive('fbind',{
//指令与元素成功绑定时(一上来)
bind(element,binding){
element.value = binding.value
},
//指令所在元素被插入页面时
inserted(element,binding){
element.focus()
},
//指令所在的模板被重新解析时
update(element,binding){
element.value = binding.value
}
})三个回调函数
配置对象中常用的3个回调函数:
bind(element,binding)
指令与元素成功绑定时调用
inserted(element,binding)
指令所在元素被插入页面时调用
update(element,binding)
指令所在模板结构被重新解析时调用
三个回调函数中的this均指window对象
备注
指令定义时不加“v-”,但使用时要加“v-”
指令名如果是多个单词,要使用kebab-case命名方式,不要用camelCase命名
new Vue({
el:'#root',
data:{
n:1
},
directives:{
'big-number'(element,binding){
element.innerText = binding.value * 10
}
}
})代码演示
局部指令演示
<body>
<!--
需求1:定义一个v-big指令,和v-text功能类似,但会把绑定的数值放大10倍。
需求2:定义一个v-fbind指令,和v-bind功能类似,但可以让其所绑定的input元素默认获取焦点。
-->
<!-- 准备好一个容器 -->
<div id="root">
<h2>当前n的值是:<span v-text="n"></span></h2>
<h2>放大10倍后n的值是:<span v-big="n"></span></h2>
<!-- <h2>放大10倍后n的值是:<span v-big-number="n"></span></h2> -->
<button @click="n++">点我n+1</button>
<hr>
<input type="text" v-fbind:value="n">
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止vue在启动时生成生产提示
new Vue({
el: '#root',
data: {
n: 1,
},
directives: {
// 自定义指令-函数式
// big函数何时会被调用?1.指令与元素成功绑定时(一上来)。2.指令所在的模板被重新解析时。
big(element, binding){
// console.log('big', this) //注意此处的this是window
element.innerText = binding.value * 10
},
fbind: {
// 指令与元素成功绑定时调用
bind(element, binding){
element.value = binding.value
},
// 指令所在元素被插入页面时调用
inserted(element, binding){
element.focus()
},
// 指令所在的模板被重新解析时调用
update(element, binding){
element.value = binding.value
element.focus()
}
}
}
})
</script>全局指令演示
<body>
<!--
需求1:定义一个v-big指令,和v-text功能类似,但会把绑定的数值放大10倍。
需求2:定义一个v-fbind指令,和v-bind功能类似,但可以让其所绑定的input元素默认获取焦点。
-->
<!-- 准备好一个容器 -->
<div id="root">
<h2>当前n的值是:<span v-text="n"></span></h2>
<h2>放大10倍后n的值是:<span v-big="n"></span></h2>
<!-- <h2>放大10倍后n的值是:<span v-big-number="n"></span></h2> -->
<button @click="n++">点我n+1</button>
<hr>
<input type="text" v-fbind:value="n">
</div>
</body>
<script type="text/javascript">
Vue.config.productionTip = false //阻止vue在启动时生成生产提示
// 定义全局指令
Vue.directive('fbind', {
bind(element, binding){
element.value = binding.value
},
inserted(element, binding){
element.focus()
},
update(element, binding){
element.value = binding.value
element.focus()
}
})
Vue.directive('big', function(element, binding){
// console.log('big', this) //注意此处的this是window
element.innerText = binding.value * 10
})
new Vue({
el: '#root',
data: {
n: 1,
}
})
</script>回顾一个dom操作
<!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>
<style>
.demo{
background-color: orange;
}
</style>
</head>
<body>
<button id="btn">点我创建一个输入框</button>
<script type="text/javascript">
const btn = document.getElementById('btn')
btn.onclick = () => {
const input = document.createElement('input')
input.className = 'demo'
input.value = 99
input.onclick = ()=> {alert(1)}
// input.parentElement.style.backgroundColor = 'skyblue' //放在这里会报错
document.body.appendChild(input)
// 只有在页面上有input时(即成功放入页面时),下面的代码才能生效,否则报错
input.focus()
input.parentElement.style.backgroundColor = 'skyblue'
console.log(input.parentElement)
}
</script>
</body>
</html>