vue3.0+ts+elementplus 封装搜索组件

<template>
  <el-form :inline="true" :model="form">
    <template v-for="item in formItems" :key="index">
      <el-form-item :label="item.label">
        <component
          :is="getComponentType(item.type)"
          v-model="form[item.field]"
          :placeholder="item.placeholder"
          :options="item.options"
          @keyup.enter="onEnterKey"
        ></component>
      </el-form-item>
    </template>
    <el-form-item>
      <el-button type="primary" @click="search">搜索</el-button>
      <el-button @click="reset">重置</el-button>
    </el-form-item>
  </el-form>
</template>
<!-- 
使用方法
     <iot-search
        :formItems="formItems"
        @search="handleSearch"
        @reset="handleReset"
        @enterKey="handleSearch"
      />


      定义
         const formItems = ref<any>([
      {
        type: "input",
        label: "名字",
        placeholder: "请输入名字",
        field: "name",
      },
      {
        type: "select",
        label: "类型",
        placeholder: "请选择类型",
        field: "type",
        options: [
          { label: "type1", value: "0" },
          { label: "type2", value: "1" },
        ],
      },
      {
        type: "select",
        label: "类型",
        placeholder: "请选择类型",
        field: "type",
        options: [], // 如果下拉框数组是从接口返回,可以使用map返回接口里面的数据,遍历formItems接收数据
      },
    ]);
-->
<script lang="ts" setup>
import { ElInput } from "element-plus";
import CustomSelect from "./CustomSelect.vue";
import { ref, defineProps, defineEmits } from "vue";

const props = defineProps({
  formItems: {
    type: Array as () => Array<{
      type: string;
      label: string;
      placeholder: string;
      field: string;
      options?: Array<{ label: string; value: string | number }>;
    }>,
    required: true,
  },
});

const emit = defineEmits(["search", "reset", "enterKey"]);

const form = ref<Record<string, any>>({});

props.formItems.forEach((item) => {
  form.value[item.field] = "";
});

const search = () => {
  emit("search", form.value);
};

const reset = () => {
  props.formItems.forEach((item) => {
    form.value[item.field] = "";
  });
  emit("reset");
};

const onEnterKey = () => {
  emit("enterKey", form.value);
};

const getComponentType = (type: string) => {
  switch (type) {
    case "input":
      return ElInput;
    case "select":
      return CustomSelect;
    default:
      return ElInput;
  }
};
</script>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
你可以尝试以下步骤来封装一个Vue 3和TypeScript下使用Element Plus的表单提交组件: 1. 安装必要的依赖: - Vue 3:`npm install vue@next` - TypeScript:`npm install -D typescript` - Element Plus:`npm install element-plus` 2. 创建一个新的Vue组件,并为其选择一个合适的名称,例如`FormSubmit.vue`。 3. 在`FormSubmit.vue`文件中,导入必要的模块和样式: ```vue <template> <!-- 表单内容 --> </template> <script lang="ts"> import { defineComponent } from 'vue'; import { ElButton, ElForm, ElFormItem } from 'element-plus'; export default defineComponent({ components: { ElButton, ElForm, ElFormItem, }, }); </script> <style scoped> /* Element Plus 样式 */ @import 'element-plus/packages/theme-chalk/src/index.scss'; /* 自定义样式 */ /* ... */ </style> ``` 4. 在模板中编写表单内容,并使用Element Plus的组件来构建表单: ```vue <template> <el-form ref="form" :model="formData" label-width="120px"> <el-form-item label="姓名" prop="name"> <el-input v-model="formData.name"></el-input> </el-form-item> <!-- 更多表单项 --> <el-form-item> <el-button type="primary" @click="submitForm">提交</el-button> </el-form-item> </el-form> </template> <script lang="ts"> // ... export default defineComponent({ // ... data() { return { formData: { name: '', // 更多表单字段 } }; }, methods: { submitForm() { // 表单提交逻辑 if (this.$refs.form.validate()) { // 表单验证通过,执行提交操作 // ... } } } }); </script> ``` 这样,你就可以使用封装好的表单提交组件来方便地处理表单提交了。你可以根据实际需求添加更多的表单项,并在`submitForm`方法中实现你的提交逻辑。希望这可以帮到你!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值