Vue自定义指令

 除了常用的指令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 只调用一次解绑时调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值