vue 自定义指令

自定义指令

Vue.directive("指令名",{});
  • Vue.directive内部钩子函数
    1). bind(){} // 绑定元素时执行,只执行一次(常用)
    2). inserted(){} // 当被绑定元素插入到dom时执行
    3). update(){} // 当被绑定元素更新时执行
    4). componentUpdated(){} // 被绑定元素模版完成一次周期更新时执行
    5). unbind(){} // 当被绑定元素解除时执行

  • Vue.directive钩子函数参数介绍
    1). el // 返回被绑定元素的dom对象
    2). binding // 返回绑定元素的各种参数object
    ps:binding实例方法:

        // 钩子函数的参数介绍
        html调用方法:<p v-world:foo.add.dele="hehe">{{msg}}</p>

        Vue.directive("world",{
            bind(el, binding){
                // console.log(el);        //  el:返回被绑定元素的dom对象
                console.log(binding);
                /*   binding 返回的各种参数
                    
                    {
                        name: "world",      // 返回自定义指令的名字
                        rawName: "v-world:foo.add.dele", // 返回自定义指令的调用
                        value: "eeeeeeeeee",        // 返回指令绑定的值
                        expression: "hehe",         // 返回指令绑定的值的名字
                        arg: "foo",         // 返回调用时参数foo的名字
                        …
                    }
                    arg: "foo"
                    expression: "hehe"
                    add: true       // 指令的修饰符
                    dele: true
                    name: "world"   // 指令的修饰符
                    rawName: "v-world:foo.add.dele"
                    value: "eeeeeeeeee" 
                */
            }
        })
  • Vue.directive第二种使用方法,直接传入一个函数,但是只会执行bing()和update()
        html调用:<p v-onefn>{{msg}}</p>
        // 传入一个函数的方法   只会执行bing()和update()钩子函数,另外指令名还不能驼峰式写法
        Vue.directive("onefn",function(){       
            alert("directive传入一个函数的方法")        
        })
  • Vue.directive局部写法
    局部写法式写在vue实例内部,而且是写为:directives,多加了一个s
    局部用法实例:
    html调用:<input type="text" v-model="msg" v-inpfocus>
    var vm = new Vue({
        el: "#app",
        data:{
            msg:"hello vue!",
        },
        methods:{
            
        },
        directives:{         // 自定义指令局部写法,局部写法记得加s为directives 全局为 Vue.directive
            inpfocus:{
                inserted(el){
                    el.focus();
                }
            }
        }
    });
自定义指令拖拽练习

点击demo查看

### 创建和使用 Vue 自定义指令 Vue 提供了自定义指令的功能,允许开发者直接操作 DOM 元素。通过自定义指令可以实现对元素的聚焦、样式修改、事件绑定等操作。 #### 注册全局自定义指令 要创建一个全局自定义指令,需要使用 `Vue.directive` 方法来注册新的指令。它接收两个参数:第一个是指令名称,第二个是指令定义对象。在定义对象中可以包含多个钩子函数[^1]: ```javascript // 注册一个名为 'focus' 的自定义指令 Vue.directive('focus', { // 指令的 inserted 钩子函数 inserted(el) { el.focus(); // 当元素插入到 DOM 时自动聚焦 } }); ``` #### 指令钩子函数 自定义指令可以通过一系列钩子函数来控制其行为。常见的钩子包括: - `bind`: 只调用一次,当指令第一次绑定到元素上时调用。 - `inserted`: 被绑定元素插入父节点时调用。 - `update`: 当虚拟节点更新但尚未应用到真实 DOM 时调用。 - `componentUpdated`: 更新完成后调用。 - `unbind`: 指令与元素解绑时调用。 例如,下面是一个用于设置文本颜色的自定义指令示例[^4]: ```javascript // 定义一个名为 'color' 的自定义指令 Vue.directive('color', { bind(el, binding) { el.style.color = binding.value; // 设置元素的颜色为传入值 }, update(el, binding) { el.style.color = binding.value; // 在数据更新时再次设置颜色 } }); ``` #### 使用局部自定义指令 除了全局注册外,还可以在组件内部定义局部自定义指令。这通常适用于特定于某个组件的行为: ```javascript export default { directives: { color: { bind(el, binding) { el.style.color = binding.value; }, update(el, binding) { el.style.color = binding.value; } } } } ``` #### 指令参数 每个钩子函数都会接收到几个参数,其中最重要的是 `el` 和 `binding`。`el` 是当前绑定的 DOM 元素,而 `binding` 对象则包含了更多关于指令的信息,如 `value`(传递给指令的值)、`oldValue`(旧值,在 `update` 和 `componentUpdated` 中可用)等。 #### 示例 - 自动聚焦 以下是一个简单的例子,演示如何创建并使用一个自动聚焦的自定义指令: ```html <template> <input v-focus /> </template> <script> export default { name: 'App' } </script> ``` 在这个例子中,输入框会在页面加载后立即获得焦点,因为应用了 `v-focus` 指令。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值