美文网首页
VUE自定义指令

VUE自定义指令

作者: 小黄不头秃 | 来源:发表于2023-06-04 19:12 被阅读0次
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <!-- 内置指令不满足需求 
         自定义指令的语法规则:
         Vue.directive(
             'focus' ,{
                 inserted:function(el){
                     el.focus();
                 }
             }
         )

         自定义指令的用法:
         <input type ="text" v-foucs>
    -->

    <!-- 
        带参数的自定义指令(改变元素的背景颜色)
                 Vue.directive('color',{
                 bind:function(el,binding){
                 console.log(binding);
                 el.style.backgroundColor = binding.value.color;
                    }
                });
     -->

     <!-- 
         局部指令:如果想要注册局部指令,组件中也接受一个directives的选项,然后就可以在模板中的任何元素中使用新的v-focus指令
         语法规则:
         directives:{
             focus:{
                 inserted:function(el){
                     el.fouces();
                 }
             }
         }
      -->
    <div id="app">
        <input type="text" v-focus>
        <input type="text" v-color="msg">
    </div>
    <script src="./vue/vue.js"></script>
    <script>
        // Vue.directive(
        //      'focus' ,{
        //         //  钩子函数
        //          inserted:function(el){
        //             //  指令所绑定的元素
        //              el.focus();
        //          }
        //      }
        //  );

        //  Vue.directive('color',{
        //      bind:function(el,binding){
        //          console.log(binding);
        //          el.style.backgroundColor = binding.value.color;
        //      }
        //  });
        var vm = new Vue({
            el:"#app",
            data:{
                msg:{
                    color:"green"
                }
            },
            methods:{},
            directives:{
             focus:{
                 inserted:function(el){
                     el.focus();
                 }
             },
             color:{
                bind:function(el,binding){
                 console.log(binding.value);
                 el.style.backgroundColor = binding.value.color;
             }
             }
         }
        });
    </script>
    
</body>
</html>

相关文章

  • season2-全局API

    第1节:Vue.directive 自定义指令 Vue.directive自定义指令 自定义的指令:changec...

  • Vue div节点滚动事件-加载更多

    使用Vue.directive注册全局指令 绑定事件 对于Vue自定义指令不明白的同学请移步: Vue自定义指令 ...

  • VUE-2:自定义指令、事件

    directive自定义指令 我们还可以通过`Vue`提供的directive方法来自定义指令 注册指令 `vue...

  • vue入门6---vue基本指令、自定义指令、插件

    一、vue常用指令概览 二、vue自定义指令 注册全局指令Vue.directive('my-directive'...

  • vue自定义指令初探

    vue自定义指令初探 一、什么是自定义指令 自定义指令是用来操作DOM的。尽管Vue推崇数据驱动视图的理念,但并非...

  • vue 有自定义指令

    vue 的自定义指令,分为全局自定义指令和局部自定义指令,局部自定义指令等价于局部组件。 自定义指令可以对DOM进...

  • Vue指令钩子函数

    Vue指令上篇文章有讲过了,现在分析Vue指令钩子函数。 自定义指令 全局定义:Vue.directive( ' ...

  • vue自定义指令

    除了内置的指令外,Vue 也允许注册自定义指令。 vue用Vue.directive(id,definition)...

  • vue知识集锦(三)

    自定义指令 除了核心功能默认内置的指令 (v-model和v-show),Vue 也允许注册自定义指令。尽管Vue...

  • Vue基础(五)--自定义指令与过渡

    1.自定义指令 分类:全局指令、局部指令 1.1 自定义全局指令 使用全局方法 Vue.directive(指令I...

网友评论

      本文标题:VUE自定义指令

      本文链接:https://www.haomeiwen.com/subject/vdkpedtx.html