除了常用的指令v-for v-shzow vue也可以自定义指令
自定义指令分为全局vue.directive 和 局部的directives
局部的和data,methods同级 全局的在main.js中定义
在标签上写 v-自定义指令名
自定义切换颜色
<p v-acolor="color" @click="change">111</p>
data() {
return {
color:'red'
};
},
methods: {
change() {
math.floor是向下取整 Math.random()是随机数
// Math.floor(Math.random()*255)
this.color=`rgb(${Math.floor(Math.random()*255)}, ${Math.floor(Math.random()*255)}, ${Math.floor(Math.random()*255)})`
}
},
directives:{
自定义指令名字 有三个参数
el绑定的元素
binding对象下面的name是指令名 value是指令的绑定的值
vnode是虚拟节点
acolor(el, binding, vnode) {
el.style.color=binding.value
}
}
自定义指令的五个钩子函数
bind只调用一次用于绑定元素
inserted:被绑定的元素到父节点是调用
update 组件更新时调用
componentupdated 组件更新后调用
unbind 只调用一次解绑时调用