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>
相关推荐
cs_dn_Jie27 分钟前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
开心工作室_kaic1 小时前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿1 小时前
webWorker基本用法
前端·javascript·vue.js
customer082 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
getaxiosluo3 小时前
react jsx基本语法,脚手架,父子传参,refs等详解
前端·vue.js·react.js·前端框架·hook·jsx
理想不理想v3 小时前
vue种ref跟reactive的区别?
前端·javascript·vue.js·webpack·前端框架·node.js·ecmascript
栈老师不回家4 小时前
Vue 计算属性和监听器
前端·javascript·vue.js
前端啊龙4 小时前
用vue3封装丶高仿element-plus里面的日期联级选择器,日期选择器
前端·javascript·vue.js
小远yyds5 小时前
前端Web用户 token 持久化
开发语言·前端·javascript·vue.js
程序媛小果5 小时前
基于java+SpringBoot+Vue的宠物咖啡馆平台设计与实现
java·vue.js·spring boot