Vue 自定义ip地址输入组件

实现效果:

组件代码

javascript 复制代码
<template>
  <div class="ip-input flex flex-space-between flex-center-cz">
    <input type="text" v-model="value1" maxlength="3" ref="ip1" :placeholder="placeholder" @input="changeToNext1(value1)"/>
    ·
    <input type="text" v-model="value2" maxlength="3" ref="ip2" :placeholder="placeholder" @input="changeToNext2(value2)"/>
    ·
    <input type="text" v-model="value3" maxlength="3" ref="ip3" :placeholder="placeholder" @input="changeToNext3(value3)"/>
    ·
    <input type="text" v-model="value4" maxlength="3" ref="ip4" :placeholder="placeholder" @input="ipAddress"/>
  </div>
</template>

<script>
export default {
  name:'IPInput',
  props:{
    placeholder:{},
    ip: {}
  },
  model: {
    prop: "ip",
    event: "change"
  },
  created(){
  },
  watch:{
    ip: {
      handler(newVal) {
        let arr = newVal.split(".")
        if ( arr.length === 4 ) {
          this.value1 = arr[0]
          this.value2 = arr[1]
          this.value3 = arr[2]
          this.value4 = arr[3]
        }
        if ( newVal === "..." ) {
          this.$emit("change", '')
        }
      },
      deep: true
    }
  },
  mounted() {
    if (this.ip == null) return;
    let arr = this.ip.split(".")
    if ( arr.length === 4 ) {
      this.value1 = arr[0]
      this.value2 = arr[1]
      this.value3 = arr[2]
      this.value4 = arr[3]
      this.$emit("change", this.value1 + "." + this.value2 + "." + this.value3 + "." + this.value4)
    }
  },
  data() {
    return {
      value1: '',
      value2: '',
      value3: '',
      value4: '',
    }
  },
  methods:{
    ipAddress() {
      this.$emit("change", this.value1 + "." + this.value2 + "." + this.value3 + "." + this.value4);
    },
    changeToNext1(v) {
      if (v.toString().length === 3) {
        this.$nextTick(() => {
          this.$refs.ip2.focus();
        });
      }
      this.$emit("change", this.value1 + "." + this.value2 + "." + this.value3 + "." + this.value4);
    },
    changeToNext2(v) {
      if (v.toString().length === 3) {
        this.$nextTick(() => {
          this.$refs.ip3.focus();
        });
      }
      this.$emit("change", this.value1 + "." + this.value2 + "." + this.value3 + "." + this.value4);
    },
    changeToNext3(v) {
      if (v.toString().length === 3) {
        this.$nextTick(() => {
          this.$refs.ip4.focus();
        });
      }
      this.$emit("change", this.value1 + "." + this.value2 + "." + this.value3 + "." + this.value4);
    }
  }
}
</script>

<style scoped>

.ip-input {
  box-sizing: border-box;
  border: 1px solid #E1DCDC;
  height: 28px;
  background-color: #FFFFFF;
}
.ip-input input {
  border: 0;
  width: 100%;
  text-align: center;
}

</style>

组件使用代码

javascript 复制代码
<template>
  <div class="root flex flex-col border-box padding-l">
    <IpInput v-model="ip" style="width: 200px;"></IpInput>
  </div>
</template>

<script>
import IpInput from '@/components/input/IpInput.vue'

export default{
  name:'',
  created() {
  },
  components: {IpInput},
  data() {
    return {
      ip:null
    }
  },
  methods:{
  }
}
</script>
相关推荐
工业互联网专业19 分钟前
基于springboot+vue的高校社团管理系统的设计与实现
java·vue.js·spring boot·毕业设计·源码·课程设计
白宇横流学长2 小时前
基于SpringBoot+Vue的旅游管理系统【源码+文档+部署讲解】
vue.js·spring boot·旅游
张人玉3 小时前
小白误入(需要一定的vue基础 )使用node建立服务器——vue前端登录注册页面连接到数据库
服务器·前端·vue.js
大大。3 小时前
element el-table合并单元格
前端·javascript·vue.js
杨.某某3 小时前
若依 v-hasPermi 自定义指令失效场景
前端·javascript·vue.js
Lysun0014 小时前
vue2的$el.querySelector在vue3中怎么写
前端·javascript·vue.js
毛毛三由4 小时前
【组件分享】商品列表组件-最佳实践
vue.js
海的预约5 小时前
VUE之路由Props、replace、编程式路由导航、重定向
前端·vue.js·智能路由器
大叔_爱编程5 小时前
wx036基于springboot+vue+uniapp的校园快递平台小程序
vue.js·spring boot·小程序·uni-app·毕业设计·源码·课程设计
小彭努力中7 小时前
16.在Vue3中使用Echarts实现词云图
前端·javascript·vue.js·echarts