分享vue2+ant-design简单封装示例

"vue": "^2.6.11",

"ant-design-vue": "^1.7.8",

import Vue from "vue";
import App from "./App.vue";
import router from "./router";

// import Antd from 'ant-design-vue';
// import "ant-design-vue/dist/antd.css";
// Vue.use(Antd);

// 按需引入Ant Design
import "ant-design-vue/dist/antd.css";
import {
  Tooltip,
  Modal,
  Tree,
  ConfigProvider,
  Icon,
  Select,
  DatePicker,
  TimePicker,
  Input,
  InputNumber,
  Row,
  Col,
  Checkbox,
  Button,
  message,
  Popconfirm,
  Form,
  Radio,
  Upload,
  Pagination,
  Cascader,
  Badge,
  Spin,
  Drawer,
  Tabs,
} from "ant-design-vue";

Vue.use(Tooltip);
Vue.use(Modal);
Vue.use(Tree);
Vue.use(ConfigProvider);
Vue.use(Icon);
Vue.use(Select);
Vue.use(DatePicker);
Vue.use(TimePicker);
Vue.use(Input);
Vue.use(InputNumber);
Vue.use(Row);
Vue.use(Col);
Vue.use(Checkbox);
Vue.use(Button);
Vue.use(Popconfirm);
Vue.use(Form);
Vue.use(Radio);
Vue.use(Upload);
Vue.use(Pagination);
Vue.use(Cascader);
Vue.use(Badge);
Vue.use(Spin);
Vue.use(Drawer);
Vue.use(Tabs);

// 使用mockjs模拟数据登录系统
import "./mock/index.js";

router.beforeEach(async (to, from, next) => {
  // 在路由拦截处获取配置 如果有责跳过 没有责调用接口获取
  if (store.state.Config || to.name === 'Home') {
    next();
  } else {
    await getConfig();
    next();
  }
});

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");
<template>
  <div :id="id">
    <a-select
      :allowClear="!allowClear"
      style="width: 100%"
      :disabled="disabled"
      :mode="multiple ? 'multiple' : 'default'"
      v-model="modelValue"
      :getPopupContainer="getPopupContainer"
      :dropdownStyle="dropdownStyle"
      @change="selectChange"
      :placeholder="placeholder"
    >
      <a-select-option
        v-for="item in dictData"
        :value="item.itemValue"
        :key="item.itemValue"
        >{
  { item.itemText }}</a-select-option
      >
    </a-select>
  </div>
</template>

<script>
import { post } from "@/request/http.js";

export default {
  mode: {
    prop: "value",
    event: "change",
  },
  props: {
    // 是否清楚按钮
    allowClear: {
      default: true,
    },
    // 字典编码
    dictCode: {
      type: String,
    },
    value: {
      type: [String, Number, Array, Object],
    },
    placeholder: {
      type: [String, Number],
      default: "请选择",
    },
    disabled: {
      type: Boolean,
      default: false,
    },
    multiple: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      id: "affirmModal" + Math.random(),
      dictData: [],
      modelValue: undefined,
      dropdownStyle: {
        background: "#173761",
        borderRadius: "0",
      },
    };
  },
  watch: {
    value: {
      handler(newVal) {
        this.modelValue = newVal;
      },
      deep: true,
      immediate: true,
    },
    dictCode: {
      handler(newVal) {
        this.getDictData();
      },
      immediate: true,
    },
  },
  created() {},
  methods: {
    selectChange(data) {
      this.$emit("change", data);
    },
    getPopupContainer() {
      return document.getElementById(this.id);
    },
    getDictData() {
      post({ url: `/dict/${this.dictCode}` }).then((res) => {
        const { data, code } = res;

        if (code === 200 && data && data.length) {
          this.dictData = data;
        } else {
          this.dictData = [];
        }
      });
    },
  },
};
</script>

<style lang="scss"
### Vue2 + Ant-Design-Vue + Formily V2 示例 以下是基于 `Vue2` 和 `Ant-Design-Vue` 的 `Formily V2` 实现的一个示例,包含筛选功能(输入框、选择框、单选按钮、Select 封装组件)以及表格展示。 #### 安装依赖 确保已安装以下依赖项: ```bash npm install vue@2.5.22 ant-design-vue@1.6.2 @formily/core@2.3.2 @formily/vue@2.3.2 ``` 如果遇到包管理器错误,请尝试清理缓存并重新安装[^4]: ```bash npm config set proxy null npm config set proxy false npm cache clean --force yarn cache clean ``` --- #### 示例代码 ##### 主文件 (`App.vue`) ```vue <template> <a-card title="筛选与表格"> <!-- 筛选区域 --> <formily-form :schema="filterSchema" /> <!-- 查询按钮 --> <div style="margin-top: 16px;"> <a-button type="primary" @click="handleSearch">查询</a-button> </div> <!-- 表格区域 --> <a-table :columns="tableColumns" :data-source="filteredData" bordered row-key="id" > <template #action="{ record }"> <span>编辑 | 删除</span> </template> </a-table> </a-card> </template> <script> import { createForm, Schema } from '@formily/core'; import { Form as FormilyForm } from '@formily/vue'; import { Input, Select, Radio } from 'ant-design-vue'; // 创建表单实例 const form = createForm(); export default { components: { FormilyForm, AInput: Input, ASelect: Select, ARadioGroup: Radio.Group, ARadioButton: Radio.Button, }, data() { return { filterSchema: new Schema({ properties: { name: { type: 'string', title: '名称', 'x-component': 'AInput', // 输入框 }, category: { type: 'string', enum: ['类别一', '类别二', '类别三'], title: '分类', 'x-component': 'ASelect', // 下拉框 }, status: { type: 'string', enum: [ { label: '启用', value: 'active' }, { label: '禁用', value: 'inactive' } ], title: '状态', 'x-component': 'ARadioGroup', // 单选按钮组 'x-component-props': { buttonStyle: 'solid' } }, }, }), tableColumns: [ { title: 'ID', dataIndex: 'id', key: 'id' }, { title: '名称', dataIndex: 'name', key: 'name' }, { title: '分类', dataIndex: 'category', key: 'category' }, { title: '操作', slots: { customRender: 'action' } }, ], rawData: [ { id: 1, name: '记录一', category: '类别一', status: 'active' }, { id: 2, name: '记录二', category: '类别二', status: 'inactive' }, { id: 3, name: '记录三', category: '类别三', status: 'active' }, ], }; }, computed: { filteredData() { const formData = this.form.values; let result = [...this.rawData]; if (formData.name) { result = result.filter(item => item.name.includes(formData.name)); } if (formData.category) { result = result.filter(item => item.category === formData.category); } if (formData.status) { result = result.filter(item => item.status === formData.status); } return result; }, }, methods: { handleSearch() { console.log('当前筛选条件:', this.form.values); }, }, }; </script> ``` --- ### 功能说明 1. **筛选功能** - 使用 `Formily` 构建动态表单,支持多种字段类型(如输入框、下拉框、单选按钮等),并通过绑定数据实现过滤逻辑[^1]。 2. **表格展示** - 基于 `Ant-Design-Vue` 的 `<a-table>` 组件渲染表格,并通过计算属性 `filteredData` 过滤显示的数据[^3]。 3. **交互设计** - 提供“查询”按钮触发筛选逻辑,同时保留原始数据以便重置或扩展其他功能。 --- ### 注意事项 - 如果运行过程中出现版本不兼容问题,请确认所使用的库版本是否匹配。例如,`@vitejs/plugin-vue` 需要 Vue 版本 >=3.2.13。 - 对于复杂场景下的性能优化,可以考虑分页加载或虚拟滚动技术来提升用户体验。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

:MNongSciFans

抛铜币以舒赞同,解兜囊以现支持

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

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

打赏作者

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

抵扣说明:

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

余额充值