vue3组件封装之搜索组件的封装

25 篇文章 1 订阅
4 篇文章 0 订阅

vue3组件封装之搜索组件的封装

码云地址 :git clone -b learn-js https://gitee.com/fsj178884799/vue3-typescript.git
组件封装环境:

1.封装好的search组件

<template>
  <a-card>
    <a-form
      :model="searchInfo"
      :label-col="labelCol"
      :wrapper-col="wrapperCol"
      layout="inline"
    >
      <div v-for="(item, index) in searchData" :key="index">
        <a-form-item v-if="item.type == 'input'" :label="item.label">
          <a-input v-model:value="searchInfo[item.value]" />
        </a-form-item>
        <a-form-item v-if="item.type == 'select'" :label="item.label">
          <a-select
            v-model:value="searchInfo[item.value]"
            :placeholder="item.placeholder"
            :options="item.menu"
            style="width: 120px"
          >
          </a-select>
        </a-form-item>
        <a-form-item v-if="item.type === 'picker'" :label="item.label">
          <a-date-picker
            v-model:value="searchInfo[item.value]"
            show-time
            type="date"
            :placeholder="item.placeholder"
            style="width: 100%"
          />
        </a-form-item>
      </div>
      <a-form-item :wrapper-col="{ span: 14, offset: 4 }">
        <a-button type="primary" @click="onSubmit">搜索</a-button>
      </a-form-item>
      <a-form-item>
        <a-button style="margin-left: 10px">重置</a-button>
      </a-form-item>
    </a-form>
  </a-card>
</template>
<script lang="ts">
import { Moment } from "moment";
import { defineComponent, reactive, toRaw } from "vue";
export default defineComponent({
  name: "search",
  props: {
    searchInfo: {
      default: {},
      type: Object,
    },
    searchData: {
      default: [],
      type: Array,
    },
  },
  setup(props: any, ctx) {
    const searchInfo: any = reactive(props.searchInfo);
    const searchData = reactive(props.searchData);
    const onSubmit = () => {
      console.log("submit!", toRaw(searchInfo));
      ctx.emit("submit-data", toRaw(searchInfo));
    };
    console.log(searchInfo);
    console.log(searchData);
    return {
      labelCol: { span: 4 },
      wrapperCol: { span: 14 },
      searchInfo,
      onSubmit,
      searchData,
    };
  },
});
</script>

2.组件的调用 组件只需要传一个searchData查询输入框相关 数组,和一个需要绑定的searchInfo的对象

<template>
  <div>
    <Search
      :searchData="searchData"
      @submitData="submit"
      :searchInfo="info"
    ></Search>
    <HelloWorld msg="Welcome to Your Vue.js + TypeScript App" />
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, toRaw } from "vue";
import HelloWorld from "@/components/HelloWorld.vue"; // @ is an alias to /src
import Search from "@/components/search.vue";

export default defineComponent({
  name: "Home",
  components: {
    HelloWorld,
    Search,
  },
  setup() {
    const searchArr = reactive([
      {
        type: "input",
        value: "name",
        label: "姓名",
        placeholder: "请输入姓名",
      },
      {
        type: "select",
        value: "sex",
        label: "性别",
        placeholder: "请输入性别",
        menu: [
          {
            value: 1,
            label: "女",
          },
          { value: 0, label: "男" },
        ],
      },
      {
        type: "picker",
        value: "date",
        label: "日期",
      },
    ]);
    const searchInfo = reactive({
      name: "",
      sex: "",
      date: "",
    });
    const submit = (value: object) => {
      console.log(value, "我是组件返回的查询条件");
    };
    const searchData = toRaw(searchArr);
    const info = toRaw(searchInfo);
    return {
      searchData,
      submit,
      info,
    };
  },
});
</script>

在这里插入图片描述
图片中的数据是search点击查询返回的数据。

  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呵呵的牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值