vue自定义指令

发布时间:2020-07-22 13:00:43 作者:811427792
来源:网络 阅读:783
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>自定义指令</title>
    </head>
    <script type="text/javascript" src="js/vue.js" ></script>
    <body>
        <div id="div1">
            <h3 v-message>{{msg}}</h3>
            <hr>
            <button @click="change">更改</button>
            <hr>
            <span v-content:header.footer="cont">{{content}}</h3>
            <hr>
            <input type="text" v-onfocus>
        </div>
    </body>
    <script>
        Vue.directive('message',{
            bind(){
                alert('指令第一次绑定到元素上时调用,只能调用一次,可执行初始化操作');
            },
            inserted(){
                alert('被绑定元素插入到DOM中时调用');
            },
            update(){
                alert('被绑定元素所在模板更新时调用');
            },
            componentUpdated(){
                alert('被绑定元素所在模板完成一次更新周期时调用');
            },
            unbind(){
                alert('指令与元素解绑时调用,只调用一次');
            }
        })

        Vue.directive('content',{
            bind(el,binding){
                console.log(el);  //打印DOM
                el.style.color = 'red';
                console.log(binding);  //打印对象
            }
        })
        let vm = new Vue({
            el: "#div1",
            data:{
                msg:"科比:你见过凌晨四点的洛杉矶吗?",
                content:"这是内容!!!",
                cont:'内容'
            },
            methods:{
                change(){
                    this.msg = '程序员:凌晨四点还没下班...';
                }
            },
            directive:{
                //局部自定义指令
                onfocus:{
                    inserted(el){
                        el.focus();
                    }
                }
            }
            
        });
        
    

    </script>
</html>


推荐阅读:
  1. Vue.js 自定义指令
  2. Vue.directive中怎么自定义指令

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

自定义 指令 vue

上一篇:Lync Server 2013 标准版部署(十)边缘服务器部署先决条件[一]

下一篇:【安全健行】(1):Hacker编程技能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》