vue中指令写了一个demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>指令</title>
    <link rel="stylesheet" href="vlib/css/index.css">
    <style>
        *{margin:0;padding:0;box-sizing:border-box;}
        a{text-decoration:none;}
    </style>
</head>
<body>
    <script src="vlib/vue.js"></script>
    <script src="vlib/index.js"></script>
    <div id="app">
        <component-child v-if="list" :mess="list"></component-child>
        <div>
            <button @click="myupdate">更新</button>
            <button @click="mydel">卸载</button>
            <button @click="myinstall">安装</button>
        </div>
    </div>
    <script>
        Vue.directive('limitStr',{
            bind:function(el,binding,vcode){
                console.log('limitStr'+1);
            },
            inserted:function(el,binding,vcode){
                console.log('limitStr'+2);
            },
            update:function(el,binding,vcode){
                console.log('limitStr'+3);
            },
            componentUpdated:function(el,binding,vcode){
                console.log('limitStr'+4);
            },
            unbind:function(el,binding,vcode){
                console.log('limitStr'+5);
            }
        })

        var componentChild = {
            template:'<div>\
                {{messval}}\
                <input type="text" :value="mess" v-limit-str>\
                </div>',
            data: function(){
              return {
                  messval:this.mess
              }
            },
            props:{
              mess:{
                  type:String,
                  default:''
              }
            },
            methods:{
                getVal:function(e){
                    this.messval = e.target.value
                }
            }
        }

        new Vue({
            el:'#app',
            data:{
                list:'hello word!!!'
            },
            components:{
                componentChild:componentChild
            },
            methods:{
                myupdate:function(){//更新
                    this.list = '更新了!!123'
                },
                mydel:function(){//卸载
                    this.list = '';
                },    
                myinstall:function(){//安装
                    this.list = 'china!!!';
                }
            }
        })
    </script>
</body>
</html>

 

转载于:https://www.cnblogs.com/zhujiasheng/p/8350595.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值