美文网首页
vue学习笔记 - 组件通信01

vue学习笔记 - 组件通信01

作者: 我永远喜欢结城明日奈 | 来源:发表于2018-02-18 21:35 被阅读0次

组件是vue最核心的功能,组件化你的前端页面,能大大的提高重用性,让代码可复用,维护起来简直爽的飞起,举例来说就比如一些评论输入框。

在vue中使用组件

Vue.component( 'input-number',{

        template:'<div>

            <input type="text" :value="currentValue" @change="handleChange">

             <button @click="handleDown" :disabled="currentValue <= min">-</button>   

             <button @click="handleUp" :disabled="currentValue >= max">+</button>

        </div> ',

        props:{

            max:{

                type:Number,

                default:Infinity

            },

            min:{

                type:Number,

                default:-Infinity

            },

            value:{

                type:Number,

                default:0

            }

        },

        data: function(){

            return { 

                        currentValue:this.value

                    }

        },

       watch:{

            currentValue:function(val){

                    this.$emit('input',val);

                    this.$emit('on-change',val);

            },

            value:function(val){

                    this.updateValue(val);

            }

        },

        methods:{

            updateValue:function(val){

                    if(val>this.max)val = this.max;

                    if(val=this.max)return;

                    this.currentValue+=1;

            },

            handleDown:function(){                 if(this.currentValue<=this.min)return;

                this.currentValue-=1;

            },

            handleUp:function(){                 if(this.currentValue>=this.max)return;         

               this.currentValue+=1;

            },

            handleChange:function(event){

                    var val = event.target.value.trim();

                    var max = this.max;

                    var min = this.min;

                    if(isValueNumber(val)){

                        val = Number(val);

                        this.currentValue = val;

                        if(val>max){

                            this.currentValue = max;

                        }else if(val<min){

                             this.currentValue = min;

                        }

                    }else{

                            event.target.value = this.currentValue;

                    }

              },

        },

        mounted:function(){            

             this.updateValue(this.value);

        }

});

function isValueNumber (value){

    return (/(^-?[0-9]+\.{1}\d+$)|(^-?[1-9][0-9]*$)|(^-?0{1}$)/).test(value);

}

引入插件

<input-number v-model="value" :max="10" :min="0"></input-number>

相关文章

  • Vue组件学习笔记(持续更新中...)

    Vue 组件学习笔记 局部组件的使用 简易 demo 组件通信 总结: 子组件通过 props 接受父组件的数据 ...

  • vue学习笔记 - 组件通信01

    组件是vue最核心的功能,组件化你的前端页面,能大大的提高重用性,让代码可复用,维护起来简直爽的飞起,举例来说就比...

  • vue文档集合

    Vue2使用笔记3--父子组件的通信Vue 2.0开发实践(组件间通讯)Event Bus 在Vue中的使用vue...

  • Vue学习笔记-组件通信

    最近公司需要开发酒店客房管理的OTA系统,我们使用 vue.js 2.0 +ElementUI 开发时 , 就遇到...

  • Vue父子组件通信

    全面学习Vue 组件通信父组件给子组件传值 props 父组件 子组件 props: { mn...

  • vue npm包的publish与install

    最近写了一个基础的Vue UI框架, 非常适合学习Vue基本知识,组件及Vue组件之间通信(涉及eventBus、...

  • vue组件之间通信

    vue 组件之间通信 vue组件之间通信方式: 1.父组件通过props向下传数据给子组件,子组件通过$emit事...

  • 【Vue】组件通信(任意通信)

    本节所需的基础知识: 【Vue】组件通信(父传子props) 【Vue】组件通信(子传父$emit) 任意组件相互...

  • vue学习笔记-组件间通信

    组件的优势:便于维护,提高开发效率,可复用,便于协同开发,便于调试。组件间的通信 父子间通信 子传父 父传子 2....

  • vue 组件通信方式 ,父子、隔代、兄弟 三类通信,六种方法

    Vue 组件间通信只要指以下 3 类通信:父子组件通信、隔代组件通信、兄弟组件通信,下面分别介绍每种通信方式且会说...

网友评论

      本文标题:vue学习笔记 - 组件通信01

      本文链接:https://www.haomeiwen.com/subject/cmbbtftx.html