自己写个vue.js插件(1):自定义指令的妙用

写个插件

文档:https://vuefe.cn/guide/plugins.html

1.在components 目录下新建一个validate.js:

export default{
    install(Vue){
        Vue.prototype.$myName = "zhagngsan";
    }
}

这就是我们的插件,定义了一个属性

2.入口文件jssrc/index.js 加入:

// 引入
import validate from "./../components/validate";
// 使用
Vue.use(validate);

3.我们到user-username.vue 组件下验证一下:

mounted(){
            alert(this.$myName);
        },

浏览器访问登录页面,成功弹出:
这里写图片描述

4.刚刚我们已经学会插件里定义属性,马上来学一下如何定义方法:

export default{
    install(Vue){
       // Vue.prototype.$myName = "zhagngsan";
        Vue.prototype.checkUserName = (value) => {
            if(/\w{6,20}/.test(value)){
                return true;
            }else{
                return false;
            }
        }
    }
}

同样可以使用该方法:

if(this.checkUserName("hello")){
    alert("ok");
}else{
    alert("error");
}

5.
这里写图片描述
我们修改user-name.vue 组件,来实现文本框验证:

<template>
    <div class="form-group">
        <label class="col-sm-2 control-label">用户名</label>
        <div class="col-sm-10">
            <input type="text" v-model="username" v-on:change="userNameChange" class="form-control" :placeholder="username">
            <label class="label label-danger" v-if="showErrorLabel">用户不合法</label>
        </div>
    </div>
</template>

<script>
    export default{

        props:["placeholder"],
        data:function () {
            return {
                username:"",
                showErrorLabel:false,
            }
        },
        methods:{
            userNameChange(){
                // 用户名改变的方法里判断 用户名是否复合要求
                if(this.checkUserName(this.username)){
                    this.showErrorLabel = false; // 如果验证没有通过就显示错误提示
                }else{
                    this.showErrorLabel = true;
                }

                // 调用父组件的方法
                this.$emit("childChange","username",this.username)
            }
        }
    }
</script>

这里写图片描述

自定义指令

文档:https://vuefe.cn/guide/custom-directive.html

1、validate.js:

export default{
    install(Vue){
       // Vue.prototype.$myName = "zhagngsan";
        Vue.prototype.checkUserName = (value) => {
            if(value == ""){
                return true; // 如果没有填写,默认为true
            }

            if(/\w{6,20}/.test(value)){
                return true;
            }else{
                return false;
            }
        }

        Vue.directive("uname",{
            bind(){
                console.log("bind"); // 只会调用一次
            },
            update(el,binding,vnode){
                console.log(el);
                console.log(binding);
                console.log(vnode);
            },
        })
    }
}

2、我们自定了一个uname 指令,下面来看一下如何使用的?

<input type="text" v-uname="username" v-model="username" v-on:change="userNameChange" class="form-control" :placeholder="username">

我们在组件的模板里使用了 v-uname ,并且给绑定了”username”数据。
我们打开浏览器的控制台:
这里写图片描述
说明我们定义的指令里,这个方法执行了:

bind(){
    console.log("bind"); // 只会调用一次
},

3、下面我们来看一下update 里的东东

update(el,binding,vnode){
    console.log(el);
    console.log(binding);
    console.log(vnode);
}

这里写图片描述

我们这里只是一个简单的展示,一定要看文档熟悉
https://vuefe.cn/guide/custom-directive.html

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值