zoukankan      html  css  js  c++  java
  • Vue 自定义指令

     value和expression区别

    第一个案列  自动获取焦点

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../vue.js"></script>
    </head>
    <body>
    <div id="app">
    同户名:<input type="text" name="userName" >
    关键字 :<input type="text" name="search" v-focus>
    </div>
    </body>
    <script>
    Vue.directive('focus',{
    bind:function (el) {

    },
    inserted:function (el) {
    el.focus();
    },
    update:function (el) {

    }
    })
    new Vue({
    el: "#app",
    data:{
    mes:"hello Vue"
    }
    });
    </script>
    </html>

                                          传参数

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../vue.js"></script>
    </head>
    <body>
    <div id="app">
    同户名:<input type="text" name="userName" >
    关键字 :<input type="text" name="search" v-focus v-color="'green'">
    </div>
    </body>
    <script>
    Vue.directive('focus',{
    bind:function (el) {
    /*不生效 dom树种还没有这个节点*/
    /*el.focus();*/
    },
                 和js行为相关的操作  最好在inserted中执行 否则js行为不生效
            inserted:function (el) {
    /*有元素的时候才能起作用*/
    el.focus();
    },
    update:function (el) {

    }
    })
    /*自定义指令 改变字体颜色*/
    Vue.directive('color',{
          和css行为相关的操作  最好在binf中执行

    
    
            bind:function (el,binding) {//binding可以随便命名但是最好规范就行 如 haha

    /*不需要关心dom树 样式只要通过指令绑定给了元素 不管这个元素有没有插入到页面中去 这个元素肯定有了内联样式*/
    /*el.style.color="red";*/
    console.log(binding.value);
    console.log(binding.expression);
    el.style.color = binding.value;
    },
    inserted:function (el) {

    },
    update:function (el) {

    }
    })
    new Vue({
    el: "#app",
    data:{
    mes:"hello Vue"
    }
    });
    </script>
    </html>
     console.log(binding.value);
    console.log(binding.expression);

    ------------------------------------------------------上面是全局指令---------------------------------------私有指令需要在其组件中定义既可以

     directives  注意有关  s

    
    
    
  • 相关阅读:
    Confluence未授权模板注入/代码执行(CVE-2019-3396)
    Python实现批量处理扫描特定目录
    windows10 缺失 msvcp140.dll 解决办法
    nessus 故障处理
    python 处理json数据
    python 实现两个文本文件内容去重
    python3 实现多域名批量访问特定目录(一)
    python 简单的实现文件内容去重
    python 实现爬取网站下所有URL
    php强大的filter过滤用户输入
  • 原文地址:https://www.cnblogs.com/jiahaoJAVA/p/9470870.html
Copyright © 2011-2022 走看看