Vue2使用v-model封装ElementUI_Input组件

简介: 本文介绍了在Vue2中如何使用v-model封装ElementUI的Input组件。封装后的组件可以根据传入的title属性决定是否显示标题,支持正则表达式校验,并提供了在Vue页面中的使用示例。

核心

可以查阅这篇文章,看一些v-model的具体实现;
简介来讲,就是自定义属性和自定义方法的结合使用。
Vue2.0、Vue3.0分别使用v-model封装组件

首先新建脚手架引入element-ui组件和样式;

新建Input.vue组件:


<template>
  <div>
    <div class="inputBox" v-if="title">
      <p :style="titleStyle" class="inputBoxP">
        {
   {
    title }}<slot name="dropdown"></slot>
      </p>
      <el-input
        :disabled="disabled"
        type
        :autosize="type === 'textarea' ? { minRows: 4, maxRows: 4 } : {}"
        class="inputBoxIpt"
        v-bind:value="value"
        :placeholder="placeholder"
        @input="inputFun"
        @blur="blurFun"
      />
      <span class="isReauireSpan" v-if="isReauired">*</span>
    </div>
    <el-input
      :disabled="disabled"
      type
      :autosize="type === 'textarea' ? { minRows: 4, maxRows: 4 } : {}"
      v-else
      class="inputBoxIpt"
      v-bind:value="value"
      :placeholder="placeholder"
      @input="inputFun"
      @blur="blurFun"
    />
    <!-- <span class="isReauireSpan" v-if="isReauired">*</span> -->
  </div>
</template>

<script>
export default {
   
  model: {
   
    prop: "value",
    event: "inputFun",
  },
  props: {
   
    titleStyle: {
   
      type: String,
      require: false,
    },
    value: {
   
      type: String,
      require: false,
    },
    title: {
   
      type: String,
      require: false,
      // default: "请传入Titlt"
    },
    placeholder: {
   
      type: String,
      require: false,
      default: "请输入",
    },
    isReauired: {
   
      type: Boolean,
      require: false,
      default: false,
    },
    type: {
   
      type: String,
      require: false,
      default: "text",
    },
    disabled: {
   
      type: Boolean,
      require: false,
      default: false,
    },
    RegExpFlag: {
   
      type: String,
      require: false,
      default: "",
    },
  },
  methods: {
   
    inputFun(e) {
   
      if (this.RegExpFlag) {
   
        this.$emit("inputFun", e.replace(eval(this.RegExpFlag), ""));
        return;
      }
      this.$emit("inputFun", e);
    },
    blurFun(e) {
   
      this.$emit("blurFun", e.target.value);
    },
  },
};
</script>

<style lang="less" scoped>
.inputBox {
   
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-start;
  align-items: center;
  width: var(--boxWidth);

  .inputBoxP {
   
    font-size: 12px;
    font-weight: 500;
    text-align: right;
    white-space: nowrap;
    width: 150px;
  }

  .inputBoxIpt {
   
    width: 200px;
  }
  /deep/.el-textarea {
   
    width: 200px !important;
  }
  /deep/.el-textarea__inner {
   
    width: 200px;
  }

  .isReauireSpan {
   
    color: #ff7875;
    font-size: 12px;
    margin-top: 8px;
    margin-left: 8px;
  }
}
</style>

我这里根据有没有传递title属性,做了渲染判断。原因在于这个组件要分别使用在form表单中和单独使用。

使用

使用肯定需要按部就班:引入_注册_页面使用。

import vInput from "@/components/elementCom/Input";

  components: {
   
    vInput,
  }

页面使用:

 <v-input v-model="msg" title="test1" />

data:

  data() {
   
    return {
   
      msg: "1",
      msg2: "2",
      msg3: "3",
      msg4: "4",
      msg5: "",
      msg6: "1",
      msg7: "2",
      msg8: ["3", "5"],
      startDate: new Date()
    };
  },

结果:
在这里插入图片描述
改变值:
在这里插入图片描述
收集输入的值:

 <el-button type="primary" @click="getAllInputFun">主要按钮</el-button>

  methods: {
   
  getAllInputFun() {
   
      console.log(this.$data,"data");
    },
  }

在这里插入图片描述
支持正则校验:

        <v-input
          RegExpFlag="/[^0-9]/g"
          titleStyle="text-align:left;width:150px"
          v-show="isShowFormItemFun('speedPercent')"
          v-model.number="speedPercent"
          title="百分比"
        >
          <template slot="dropdown">
            <el-dropdown @command="handleCommandspeedPercent" trigger="click">
              <span
                class="el-dropdown-link"
                :title="
                  commandFlagspeedPercent === '0' ? '大于等于' : '小于等于'
                "
              >
                {
  { commandFlagspeedPercent === "0" ? "≥" : "≤"
                }}<i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item title="大于等于" command="0"
                  ></el-dropdown-item
                >
                <el-dropdown-item title="小于等于" command="1"
                  ></el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </v-input>
目录
相关文章
|
JavaScript Go 数据安全/隐私保护
【Vue】组件封装——input输入框
【Vue】组件封装——input输入框
238 0
【Vue】组件封装——input输入框
|
1天前
|
JavaScript
Vue2使用v-model封装ElementUI_CheckBox组件
本文介绍了在Vue2中如何使用v-model封装ElementUI的CheckBox组件。封装后的组件支持有标题和无标题的情况,并提供了在Vue页面中的使用示例,包括单独使用和在表单中的使用。
13 2
|
2月前
|
JavaScript 编译器
|
4月前
|
JavaScript 开发者
[vue2/vue3] -- 深入剖析v-model的原理、父子组件双向绑定的多种写法
[vue2/vue3] -- 深入剖析v-model的原理、父子组件双向绑定的多种写法
[vue2/vue3] -- 深入剖析v-model的原理、父子组件双向绑定的多种写法
|
4月前
vue2 系列:自定义 v-model
vue2 系列:自定义 v-model
36 0
|
4月前
|
JavaScript
Vue中的v-model如何实现一个自定义组件的双向绑定?
Vue中的v-model如何实现一个自定义组件的双向绑定?
139 2
|
4月前
|
JavaScript 开发者
Vue中v-model的原理是什么?
Vue中v-model的原理是什么?
45 0
|
4月前
|
JavaScript
【双向绑定极简版代码】在Vue.js的自定义组件中实现v-model=“”双向绑定
【双向绑定极简版代码】在Vue.js的自定义组件中实现v-model=“”双向绑定
|
JavaScript
vue动态生成表单组件vue-form-maker
vue动态生成表单组件vue-form-maker
169 0
|
JavaScript
Vue 自定义组件实现 v-model 数据双向绑定
Vue 自定义组件实现 v-model 数据双向绑定
119 0