• vue实现自定义的v-xxx指令


    有时候,我会比较疑惑,v-show  v-if这些指令都是怎么写出来的,那么今天我就和大家一起看一下,如何自己写一个自己的指令,比如就叫v-xxx指令吧。

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <script src="./js/vue.js"></script>
    </head>
    <body>
        <div id="app">
             <input type="button" value="++" @click="countAdd"/>
             <input type="button" value="--" @click="countSub"/>
             {{count}}
             <h1 v-xxx="count">如果不++,看不见我</h1>
        </div>
        <script>
            var m = {
                 count : 0
            }
     
            var vm = new Vue({
                el:"#app",
                data: m,
                methods:{
                     countAdd:function(){
                         this.count++;
                     },
                     countSub:function(){
                         this.count--;
                     }
                },
                directives:{
                    xxx:{
                        bind:function(el,bindings){
                            console.log('binding......');
                            console.log(el,bindings);
                            if(bindings.value > 0){
                                el.setAttribute('style','display:block');
                            }else{
                                el.setAttribute('style','display:none');
                            }
                        },
                        update:function(el,bindings){
                            console.log('updating......');
                            console.log(el,bindings);
                            if(bindings.value > 0){
                                el.setAttribute('style','display:block');
                            }else{
                                el.setAttribute('style','display:none');
                            }
                        },
                        unbind:function(el,bindings){
    
                        }
                    }
                }
            })
        </script>
     
    </body>
    </html>
    

      

     

     我们实现了一个指令 v-xxx

    这个指令的功能是,当v-xxx绑定的值是0时候,隐藏v-xxx所在的控件,如果v-xxx绑定的大于0时,显示v-xxx所在的控件

  • 相关阅读:
    hdu_2224_The shortest path(dp)
    hdu_4824_Disk Schedule(dp)
    hdu_5680_zxa and set(想法题)
    hdu_5683_zxa and xor(非正解的暴力)
    hdu_1429_胜利大逃亡(续)(BFS状压)
    hdu_1254_推箱子(双BFS)
    hdu_1969_pie(二分)
    hdu_2446_Shell Pyramid(数学,二分)
    hdu_2141_Can you find it?(二分)
    5.2 nc + JMX查看分布式程序数据
  • 原文地址:https://www.cnblogs.com/lukairui/p/14445531.html
Copyright © 2020-2023  润新知