VUE3封装EL-ELEMENT-PLUS input组件

VUE3封装EL-ELEMENT-PLUS input组件

完整代码

<template>
  <div>
    <div>
      <div class="lable_top" v-if="label">
        <label :class="lable_sty">{{ label }}</label>
      </div>
      <el-input
        v-model="inputValue"
        @input="emitInput"
        :clearable="clearables"
        :style="{ width: width }"
        :rows="height"
        :type="textType"
        :class="input_sty"
        :placeholder="placeholder"
        :maxlength="maxlength"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, watch, toRefs } from "vue";
import {
  btKeyUpSpecChar,
  isChina,
  isNumber,
  isLetterAndNumber,
} from "@/utils/verification";

const state = reactive({
  lable_sty: "lable_sty",
  input_sty: "input_sty",
  lable_width: " ",
});

const { lable_sty, input_sty, lable_width } = toRefs(state);
const Rulecheck = ref(null);
const clearables = ref();
onMounted(() => {
  VerifyTextType();
  lable_width.value = props.lable_width;
  Rulecheck.value = props.verifyText;
  // 清除按钮
  if (props.clearable == "true") {
    clearables.value = true;
  } else if (props.clearable == "false") {
    clearables.value = false;
  } else {
    console.log("clearable输入有误:", props.clearable);
  }
});
const emit = defineEmits(["update:modelValue"]);
let props = defineProps({
  width: {
    type: String,
    default: "200px",
  },
  value: {
    type: String,
    default: "",
  },
  height: {
    type: Number,
    default: 1,
  },
  label: {
    type: String,
  },
  lable_width: {
    type: String,
    default: "100px",
  },
  placeholder: {
    type: String,
    default: "",
  },
  maxlength: {
    type: Number,
    default: 15,
  },
  verifyText: {
    type: String,
  },
  clearable: {
    type: String,
    default: "true",
  },
});

// 高度
const textType = ref("text");
function VerifyTextType() {
  if (props.height > 1) {
    textType.value = "textarea";
    lable_sty.value = "lable_sty1";
    input_sty.value = "input_sty1";
  } else {
    textType.value = "text";
    lable_sty.value = "lable_sty";
    input_sty.value = "input_sty";
  }
}

// 监听文本框输入
const inputValue = ref(props.modelValue);
watch(
  () => props.modelValue,
  (newValue) => {
    inputValue.value = newValue;
  }
);
const emitInput = () => {
  // 多个正则匹配
  if (Rulecheck.value) {
    var multi = Rulecheck.value.split(",");
    for (let index = 0; index < multi.length; index++) {
      switch (multi[index]) {
        case "isLetterAndNumber":
          inputValue.value = isLetterAndNumber(inputValue.value);
          break;
        case "isChina":
          inputValue.value = isChina(inputValue.value);
          break;
        case "isNumber":
          inputValue.value = isNumber(inputValue.value);
          break;
        default:
          break;
      }
    }
  }
  inputValue.value = btKeyUpSpecChar(inputValue.value);
  emit("update:modelValue", inputValue.value);
};
</script>

<style lang="scss" scoped>
.lable_top {
  display: inline-block;
  width: v-bind(lable_width);
  text-align: right;
}
.lable_sty {
  font-weight: normal !important;
  font-size: 14px;
  color: #606266;
  display: inline-block;
}
.lable_sty1 {
  font-weight: normal !important;
  font-size: 14px;
  color: #606266;
  display: inline-block;
  vertical-align: text-top;
}
.input_sty {
  margin-left: 10px;
}
.input_sty1 {
  margin-left: 10px;

  vertical-align: text-top;
}
</style>

使用

<template>
  <div class="app-container">
    <general_inpuut v-model="acc" clearable="false" />
    <general_inpuut label="船舶编码:" v-model="acc" width="500px" />
    <general_inpuut
      label="测试2:"
      v-model="acc"
      width="600px"
      placeholder="请输入备注"
      maxlength="100"
      height="3"
      verifyText="isNumber"
    />

    <el-button @click="btn_change">获取值</el-button>
  </div>
</template>

<script setup>
const acc = ref();

function btn_change() {
  console.log("acc.value", acc.value);
}
</script>
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面我来尝试给您讲解一下如何基于若依框架使用vue3+element-plus去二次封装一个el-dialog: 1. 首先,在使用vue3+element-plus进行开发前,需要先安装对应的依赖。可以通过以下命令进行安装: ``` npm install vue@next npm install element-plus --save ``` 2. 接下来,我们需要创建一个新的dialog组件。可以在`components`文件夹下新建一个`dialog.vue`文件,然后在该文件中定义我们的dialog组件。 ```vue <template> <el-dialog :visible.sync="visible" :title="title" :width="width" :before-close="beforeClose"> <slot></slot> </el-dialog> </template> <script> import { defineComponent } from 'vue' import { ElDialog } from 'element-plus' export default defineComponent({ name: 'Dialog', components: { ElDialog, }, props: { visible: { type: Boolean, default: false, }, title: { type: String, default: '', }, width: { type: String, default: '50%', }, beforeClose: { type: Function, default: () => {}, }, }, }) </script> ``` 在`dialog`组件中,我们使用了`<el-dialog>`组件,并且对其进行了二次封装。我们将`visible`、`title`、`width`和`beforeClose`四个属性绑定到`<el-dialog>`组件上,同时在`<slot>`中插入了组件传递进来的内容。 3. 接下来,我们需要在使用dialog的页面中引入该组件,并且传递需要的参数。 ```vue <template> <div> <el-button @click="showDialog">打开dialog</el-button> <dialog :visible="dialogVisible" :title="dialogTitle" :beforeClose="beforeClose"> <el-form> <el-form-item label="姓名"> <el-input v-model="name"></el-input> </el-form-item> <el-form-item label="年龄"> <el-input v-model="age"></el-input> </el-form-item> </el-form> </dialog> </div> </template> <script> import { defineComponent, ref } from 'vue' import Dialog from '@/components/dialog.vue' export default defineComponent({ name: 'Page', components: { Dialog, }, setup() { const dialogVisible = ref(false) const dialogTitle = ref('示例dialog') const name = ref('') const age = ref('') const showDialog = () => { dialogVisible.value = true } const beforeClose = (done) => { if (name.value && age.value) { done() } else { this.$message.warning('请输入完整信息') } } return { dialogVisible, dialogTitle, name, age, showDialog, beforeClose, } }, }) </script> ``` 在`Page`页面中,我们引入了刚才定义的`<dialog>`组件,并且传递了需要的参数。在点击打开dialog的按钮时,我们通过`showDialog`方法来显示dialog。在dialog关闭前,我们通过`beforeClose`方法来进行校验,只有当输入完整信息时才能关闭dialog。 至此,我们就完成了基于若依框架使用vue3+element-plus去二次封装一个el-dialog的操作。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值