vue-自定义指令

  1. 自定义指令介绍 directives
  2. 钩子函数
  • 参数 el,binding,vnode(vnode.context)
  • bind,inserted,update,componentUpdated,unbind
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <title>Examples</title>
    <meta name="description" content="">
    <meta name="keywords" content="">
    <link href="" rel="stylesheet">
    <script type="text/javascript" src="lib/vue.js"></script>
</head>

<body>
    <div id="box">
        <div v-hello="'red'">111111</div>
        <!--"red"会被当成变量-->
        <div v-hello="'yellow'">2222222</div>
        <div v-hello="mycolor">3333333</div>
    </div>

    <script type="text/javascript">
        // v-if="isShow" v-for v-model v....
        // v-kerwin v-swipe  操作底层dom   自定义指令
        Vue.directive("hello", {
            inserted(el, bind) {
                // 指令-生命周期-创建
                // inserted()插入 
                // console.log("当前节点插入到父节点了")  //测试
                // console.log(el);//底层
                // console.log(bind);//对象
                // console.log(bind.value);
                el.style.background = bind.value
            },
            update(el, bind) {
                // 指令-生命周期-更新
                el.style.background = bind.value
            }
        })
        var vm = new Vue({
            el: "#box",
            data: {
                mycolor: "red"
            }
        })

    </script>
</body>
</html>
vm.mycolor="yellow"
"yellow"
vm.mycolor="blue"
"blue"
vm.mycolor="grey"
"grey"
vm.mycolor="green"
"green"
vm.mycolor="orange"
"orange"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值