美文网首页墨海文章集
vue学习(25)自定义指令

vue学习(25)自定义指令

作者: 哆啦C梦的百宝箱 | 来源:发表于2022-03-10 11:33 被阅读0次
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>自定义指令</title>
</head>

<body>
    <div id="root">
        <!-- 1:定义一个v-big指令,和v-text类似,只是把内容放大10倍 -->
        <!-- 2: 定义一个v-fbind指令,和v-bind类似,可以让input默认获取焦点-->
        <h2>当前的n值是:<span v-text="n"></span></h2>
        <h2>放大10倍后的n值是:<span v-big-number="n"></span></h2>
        <button @click="n++">点我n+1</button>
        <input type="text" v-fbind:value="n">
    </div>
    <script src="../js/vue.js"></script>
    <script type="text/javascript">
        // 全局
        //Vue.directive('xxx',function(){})
        const vm = new Vue({
            el: '#root',
            data: {
                n: 1
            },
            directives: {
                'big-number'(element, binding) {
                    //注意此处的this为window
                    //big函数何时调用:1指令和元素成功绑定时2:指令所在的模板被重新解析。
                    // element是真实dom。
                    console.log(element instanceof HTMLElement);
                    element.innerText = binding.value * 10;
                },
                fbind: {
                    //指令与元素绑定(一上来)
                    bind(element, binding) {
                        element.value = binding.value;
                    },
                    //指令所在的元素被插入页面
                    inserted(element, binding) {
                        element.focus();
                    },
                    //指令所在的模板被重新解析
                    update(element, binding) {
                        element.value = binding.value;
                    }
                }

            }
        })
    </script>
</body>

</html>
知识点

1:局部指令
new Vue({
directives:{指令名,配置对象}
})
new Vue({
directives:{指令名,回调函数}
})
2:全局指令
Vue.directive(指令名,配置对象)
Vue.directive(指令名,回调函数)
3:配置对象中常用的三个回调
①:bind:指令与元素成功绑定时。
②:inserted:指令所在的元素被插入页面时。
③:update :指令所在的模板被重新解析时。
4:定义指令时不加v-,使用时加v-。
5:指令名是多个单词时,要使用kebab-case的格式,不要使用camelCase。函数名需要加上引号。

相关文章

  • season2-全局API

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

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

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

  • Vue-02:

    一.Vue的指令: 分为两大类;内置指令和自定义指令;今天我们就来学习一下Vue的内置指令; 1.>v-for :...

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

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

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

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

  • vue自定义指令初探

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

  • vue 有自定义指令

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

  • vue学习(25)自定义指令

    知识点 1:局部指令new Vue({directives:{指令名,配置对象}})new Vue({direct...

  • Vue指令钩子函数

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

  • vue自定义指令

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

网友评论

    本文标题:vue学习(25)自定义指令

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