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>
相关推荐
qbbmnnnnnn28 分钟前
【WebGis开发 - Cesium】如何确保Cesium场景加载完毕
前端·javascript·vue.js·gis·cesium·webgis·三维可视化开发
杨荧2 小时前
【JAVA开源】基于Vue和SpringBoot的水果购物网站
java·开发语言·vue.js·spring boot·spring cloud·开源
霸王蟹3 小时前
Vue3 项目中为啥不需要根标签了?
前端·javascript·vue.js·笔记·学习
老章学编程i4 小时前
Vue工程化开发
开发语言·前端·javascript·vue.js·前端框架
tanxiaomi5 小时前
vue 不是spa 单页面应用吗? 配置路由工作模式为history 后 ,为什么配置Nginx的 try_files 可以根据url 找到对应的文件?
前端·vue.js·nginx
果子切克now5 小时前
vue3导入本地图片2种实现方法
前端·javascript·vue.js
二十雨辰7 小时前
[uni-app]小兔鲜-06地址+sku+购物车
前端·javascript·vue.js·uni-app
DT——8 小时前
vue中如何实现组件通信
前端·javascript·vue.js
开心工作室_kaic9 小时前
基于微信小程序的校园失物招领系统的设计与实现(论文+源码)_kaic
c语言·javascript·数据库·vue.js·c#·旅游·actionscript
customer0810 小时前
【开源免费】基于SpringBoot+Vue.JS洗衣店订单管理系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源