首页 > 其他 > 详细

Vue 自定义指令

时间:2021-03-17 00:25:10      阅读:47      评论:0      收藏:0      [点我收藏+]
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>自定义指令</title>
    <script src="./vue-v2.6.10.js"></script>
</head>
<body>
    <div id="app">
        <p v-red>123</p>
        <div v-red>4343455</div>
    </div>
</body>
<script>
    // 全局自定义指令
    // Vue.directive(‘red‘,{
    //     inserted:function(el){
    //         el.style.background = ‘red‘;
    //     }
    // })

    var app = new Vue({
        el:#app,
        data:{
            
        },
        // 局部自定义指令
        directives:{
            red:{
                inserted:function(el){
                    el.style.color = green;
                }
            }
        }
    });

</script>
</html>

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <script src="./vue-v2.6.10.js"></script>
</head>
<body>
    <div id="app">
        <p v-color="msg">123</p>
        <!-- <input type="text" v-model="msg"> -->
    </div>
</body>
<script>
    // 全局自定义指令
    // Vue.directive(‘red‘,{
    //     inserted:function(el){
    //         el.style.background = ‘red‘;
    //     }
    // })

        // 自定义指令的值需要现有数据才能使用

    var app = new Vue({
        el:#app,
        data:{
            msg:red
        },
        // 局部自定义指令
        directives:{
            color:{
                inserted:function(el,data){
                    el.style.color = data.value;
                    // console.log(data);
                }
            }
        }
    });

</script>
</html>

 

Vue 自定义指令

原文:https://www.cnblogs.com/dazahui/p/14546814.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!