VUE3+Element-Plus form表单封装

VUE3+Element-Plus form表单封装

新建form组件页面

在components中创建新组件,将需要的form表单中常用的UI组件引入;
vue3创建组件和vue2中多少有点区别,但是影响不是很大,也好理解;
获取到父组件传过来的数据,循环el-form-item标签,我这里是为了满足UI设计图需求,循环了布局容器中的el-col标签,固定数值,循环超出会自动换行,不用担心页面错乱

<template>
  <form :model="props.ruleForm">
    <el-row :gutter="20">
      <el-col :span="8" v-for="item in props.formData" :key="item.key">
        <el-form-item :label="item.label" :prop="item.key"  label-width="120px">
          <!-- 输入框 -->
          <el-input
            v-if="item.type == 'input'"
            v-model="props.ruleForm[item.key]"
            :placeholder="item.placeholder"
            clearable
          />
          <!-- 下拉选择框 -->
          <el-select
            v-if="item.type == 'select'"
            v-model="props.ruleForm[item.key]"
            :placeholder="item.placeholder"
            clearable
          >
            <el-option
              v-for="items in item.list"
              :key="items.value"
              :label="items.label"
              :value="items.value"
            />
          </el-select>
          <!-- 时间选择 -->
          <el-date-picker
            v-if="item.type == 'date'"
            v-model="props.ruleForm[item.key]"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始时间"
            end-placeholder="结束时间"
            format="YYYY-MM-DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-col>
    </el-row>
  </form>
</template>

<script setup>
import { ref, reactive } from 'vue'
const props = defineProps({
  formData: {
    type: Array,
    default: []
  },
  ruleForm: {
    type: Object,
    default: {}
  }
})
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
.el-form-item,
.el-input,
.el-form-item__label,
.el-date-editor {
  height: 40px !important;
  line-height: 40px !important;
}
</style>

创建index.vue

页面中直接引入form组件,数组form.formData中,根据需要的组件写入对应的type值,方便组件判断;
form.ruleForm的作用在于方便获取组件页的数据这里不做过多解释(代码复制可直接使用查看)

<template>
  <div>
    <div class="wrap_top">
      <s-form :formData="form.formData" :ruleForm="form.ruleForm"></s-form>
      <div class="jut">
        <div class="wrap_btn">
          <el-button type="primary" @click="tatps">筛选</el-button>
          <el-button plain>导出</el-button>
          <el-button type="info" plain>重置</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
//引入form封装组件
import sForm from '@/components/forms'
import { ref, reactive} from 'vue'
let calssOptions = reactive([
  {
    label: '测试',
    value: 'A'
  }
])
let ageOptions = reactive([])
let TypeOptions = reactive([])
let form = reactive({
  formData: [
    {
      type: 'input',
      key: 'item',
      label: '图书编码',
      placeholder: '请输入/扫描图书编码'
    },
    {
      type: 'select',
      key: 'classify_id',
      label: '图书类别',
      placeholder: '请选择图书类别',
      list: calssOptions
    },
    {
      type: 'date',
      key: 'gmt_create_start',
      label: '入库时间',
      placeholder: '请选择图书状态'
    }
  ],
  ruleForm: {
    gmt_create_start: []
  }
})
const page = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 筛选
const tatps = () => {
	console.log(form.ruleForm)
}

</script>

<style lang="scss" scoped>
.wrap_top {
  background: #fff;
  padding: 15px;
  border-radius: 8px;
}
</style>

严格来说第一次写博客,大家有什么不合适的可以直接私信或者评论,我有时间会回复,也可以讨论讨论技术,最后,谢谢大家阅览

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Vue 3.0+Element-Plus开发后台管理系统的步骤: 1.首先,确保你已经安装了Node.js和npm包管理器。 2.使用以下命令安装Vue CLI 4: ```shell npm install -g @vue/cli ``` 3.使用Vue CLI创建一个新项目: ```shell vue create my-project ``` 4.在创建项目时,选择使用Vue 3.0版本,并启用class-style component语法: ```shell ? Please pick a preset: Manually select features ? Check the features needed for your project: Choose Vue version, Babel, Router, Vuex, CSS Pre-processors, Linter, Unit ? Choose a version of Vue.js that you want to start the project with 3.x (Preview) ? Use class-style component syntax? Yes ? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? No ? Use history mode for router? (Requires proper server setup for index fallback in production) Yes ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass) ? Pick a linter / formatter config: ESLint with error prevention only ? Pick additional lint features: Lint on save ? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files ``` 5.安装Element-Plus和Echarts 5.0: ```shell npm install element-plus echarts@5.0 ``` 6.在main.js中引入Element-Plus和Echarts: ```javascript import { createApp } from 'vue' import App from './App.vue' import router from './router' import store from './store' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' import echarts from 'echarts' const app = createApp(App) app.use(store) app.use(router) app.use(ElementPlus) app.config.globalProperties.$echarts = echarts app.mount('#app') ``` 7.现在你可以开始使用Element-Plus和Echarts来开发你的后台管理系统了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值