Vue自定义input区间组件

疫情期间,在家用两个input实现一个区间组件,希望对你们有帮助。效果图如下:
在这里插入图片描述
使用步骤:
一,在项目中创建组件InputRange.vue文件,把以下代码复制到新建组件中。

<template>
  <div class="hy-input-wrapper" :style="myStyle">
    <div  class="hy-input">
        <input :type="type" :placeholder="startPlaceholder" v-model="inputMap.start"  @input="inputValue"  autocomplete="off"  class="hy-input__inner" >
    </div>
    <div class="connect">{{connectText}}</div>
    <div  class="hy-input">
      <input :type="type" :placeholder="endPlaceholder" v-model="inputMap.end"  @input="inputValue" class="hy-input__inner" >
    </div>
  </div>
</template>
<script>
  export default {
    data(){
      return {
          inputMap: {...this.value}
      }
    },
    props: {
        startPlaceholder: {type: String, default: '起始值'},
        endPlaceholder: {type: String, default: '结束值'},
        type: {type: String, default: 'text'},
        connectText: {type: String, default: '至'},
        value: {type: Object, default: function(){
          return {start: '',end: ''}
          }
        },  //v-model绑定的值
        myStyle: {type: Object, default: function(){
          return  {width: '580px'}
         }
       }
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    watch: {
      'value.start'(val) {
          this.inputMap.start = val;
      },
      'value.end'(val) {
          this.inputMap.end = val;
      }
    },
    created(){ },
    mounted(){ },
    methods: {
        inputValue(event){
          this.$emit("change",{...this.inputMap})   
        }
    }
  }
</script>
<style scoped>
  .hy-input-wrapper{
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  }
  .connect{
    font-size: 14px;
    font-weight: bold;
    color: #606266
  }
  .hy-input {
      position: relative;
      font-size: 13px;
      display: inline-block;
      padding: 0 10px 0 10px;
      width: 100%;
  }
  .hy-input__inner {
    -webkit-appearance: none;
    background-color: #FFF;
    background-image: none;
    border-radius: 4px;
    border: 1px solid #DCDFE6;
    -webkit-box-sizing: border-box;
    box-sizing: border-box;
    color: #606266;
    display: inline-block;
    font-size: inherit;
    height: 32px;
    line-height: 32px;
    outline: 0;
    padding: 0 15px;
    -webkit-transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    transition: border-color .2s cubic-bezier(.645,.045,.355,1);
    width: 100%;
}
.hy-input__inner:focus {
    border-color: #409EFF;
    outline: 0;
}
</style>

二,注册组件

import InputRange from './components/InputRange'  //导入组件,注意路径
Vue.component('input-grange',InputRange)  //注册

三,使用

<input-grange v-model='rangeValue'> </input-grange>
  data() {
        return {
          rangeValue: {start: '', end: ''},  //用来绑定
         }
      }

相关属性:

参数说明类型可选值默认值
startPlaceholder第一个输入框占位文本string起始值
endPlaceholder第二个输入框占位文本string结束值
type文本类型stringnumber,texttext
connectText连接符,连接两个input的字符string
myStyle组件的样式Object{width: ‘280px’}
  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值