• [Vue] 31.混入与自定义指令:编写自定义指令(2)


    一、自定义指令绑值
    实现输入框的样式距页面顶部指定像素

    1. 定义一个全局指令pos:binding变量可以接收元素传过来的值
    app.directive('pos', {
                mounted(el, binding) {
                    el.style.top = (binding.value + 'px')
                }
            })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    1. 元素使用自定义指令:
    const app = Vue.createApp({
                template: `
                <div>
                    <div v-pos="400" class="header">
                        <input />
                    </div>
                </div>
                `
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    完整代码如下:

    <!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>29</title>
        <style>
            .header {
                position: absolute
            }
        </style>
        <script src="https://unpkg.com/vue@next"></script>
    </head>
    
    <body>
        <div id="root"></div>
        <script>
            // 自定义指令 directive
            const app = Vue.createApp({
                template: `
                <div>
                    <div v-pos="400" class="header">
                        <input />
                    </div>
                </div>
                `
            });
            app.directive('pos', {
                mounted(el, binding) {
                    el.style.top = (binding.value + 'px')
                }
            })
            const vm = app.mount('#root')
        </script>
    </body>
    </html>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    1. 也可以将数据放到data中:
    const app = Vue.createApp({
                data() {
                    return {
                        top: 100
                    }
                },
                template: `
                <div>
                    <div v-pos="top" class="header">
                        <input />
                    </div>
                </div>
                `
            });
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    注:

    app.directive('pos', (el, binding) => {
                el.style.top = (binding.value + 'px')
            })
    
    • 1
    • 2
    • 3

    等价于:

    app.directive('pos', {
                mounted(el, binding) {
                    el.style.top = (binding.value + 'px')
                },
                updated(el, binding) {
                    el.style.top = (binding.value + 'px');
                }
            })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    01-获取配置文件路径
    多特征线性回归
    Go 语言中 For 循环:语法、使用方法和实例教程
    『忘了再学』Shell基础 — 32、Shell中test测试命令详解
    明日周刊-第1期
    小米手环8pro重新和手机配对解决办法
    hyperf笔记
    GET和POST请求的区别
    恭喜马斯克、纳德拉当选美国工程院院士,张宏江、方岱宁入选外籍院士
    04 卷积神经网络搭建
  • 原文地址:https://blog.csdn.net/u014627255/article/details/125624194