vue3+element-plus+js 对列表查询/重置条件 组件简单封装

在写后台管理的时候会有很多列表,列表上面一般会有查询条件,对列表进行搜索查询,所以就想封装成为组件,就不需要每个页面写一堆的代码,直接循环出来进行遍历即可。

1.封装子组件searchForm组件 

 

<template>
  <el-form :model="searchInfo">
    <el-row :gutter="10" style="margin: 10px 0;">
      <el-col :span="item.span || 6" v-for="item, i in searchData" :key="i">
        <el-form-item :label="item.label" :label-width="item.width || '80px'">
          <el-input v-if="item.type === 'input'" v-model="searchInfo[item.prop]"
            :placeholder="item.placeholder || '请输入'" />
          <el-select v-if="item.type === 'select'" v-model="searchInfo[item.prop]"
            :placeholder="item.placeholder || '请选择'">
            <el-option v-for="item2, i2 in item.options" :key="i2" :label="item2.label" :value="item2.value" />

          </el-select>
          <el-date-picker v-if="item.type === 'date'" type="date" :value-format="item.format || 'YYYY-MM-DD'"
            :placeholder="item.placeholder || '请选择日期'" v-model="searchInfo[item.prop]"
            style="width: 100%;"></el-date-picker>
        </el-form-item>
      </el-col>

      <el-col :span="btnInfo.span || 6">
        <el-row justify="end">
          <el-button :type="btnInfo.query || 'primary'" @click="onSubmit">查询</el-button>
          <el-button :type="btnInfo.reset || 'default'" @click="onSubmit('reset')">重置</el-button>
        </el-row>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup>
const props = defineProps({
  //搜索信息
  searchInfo: {
    type: Object,
    default: {}
  },
  //表单数组
  searchData: {
    type: Array,
    default: []
  },
  //按钮信息
  btnInfo: {
    type: Object,
    default: {}
  }
})

const emit = defineEmits(['submitEmits']);
const onSubmit = (data) => {
  if (data == 'reset') {
    emit('submitEmits', {});
  } else {
    emit('submitEmits', props.searchInfo);
  }

}
</script>

<style lang="scss" scoped></style>

一般搜索条件都是输入框、选择器、日期选择,如有其他筛选条件,往里面添加即可!搜索时发射一个自定义事件,把数据传给父组件调用即可

2.父组件进行使用

<template>
  <searchForm :searchData="searchData" :searchInfo="productionlineForm" @submitEmits="submitEmitsFn"></searchForm>
</template>

<script setup>

import searchForm from '@/components/searchForm/index.vue'

let productionlineForm = ref({
  lineName: '',
  lineCode: '',
  status: ''
})

const searchData = ref([
  {
    label: '产线名称',
    type: 'input',
    prop: 'lineName'
  },
  {
    label: '产线编号',
    type: 'input',
    prop: 'lineCode'
  },
  {
    label: '状态',
    type: 'select',
    prop: 'status',
    options: [
      {
        label: '正常',
        value: 0
      },
      {
        label: '停用',
        value: 1
      }
    ]
  }
])


const submitEmitsFn = (val) => {
  productionlineForm.value = val
  //调接口渲染数据
  //getLinePageFn()
}


</script>

<style lang="scss" scoped></style>

 

### 如何在 Vue 3 中使用 Element Plus 实现用户登录功能 #### 安装依赖 为了实现基于 Vue 3 的用户登录页面,首先需要安装必要的依赖项。可以通过以下命令初始化项目并引入 `Element Plus`: ```bash npm init vite@latest my-element-plus-app --template vue cd my-element-plus-app npm install element-plus axios pinia ``` 此过程涵盖了项目的创建以及所需库的安装[^3]。 --- #### 配置 Element Plus 在项目中全局注册 `Element Plus` 是常见的做法。可以在 `main.js` 文件中完如下配置: ```javascript import { createApp } from &#39;vue&#39;; import App from &#39;./App.vue&#39;; import ElementPlus from &#39;element-plus&#39;; import &#39;element-plus/dist/index.css&#39;; const app = createApp(App); app.use(ElementPlus); app.mount(&#39;#app&#39;); ``` 通过这种方式可以确保组件样式正常加载[^1]。 --- #### 路由设置 定义路由以便导航到不同的视图。以下是针对登录和查询产品的简单路由配置示例: ```javascript // router/index.js import { createRouter, createWebHistory } from &#39;vue-router&#39;; import LoginRegister from &#39;../components/LoginRegister.vue&#39;; import ProductQuery from &#39;../components/ProductQuery.vue&#39;; const routes = [ { path: &#39;/login-register&#39;, name: &#39;LoginRegister&#39;, component: LoginRegister, }, { path: &#39;/product-query&#39;, name: &#39;ProductQuery&#39;, component: ProductQuery, }, ]; const router = createRouter({ history: createWebHistory(), routes, }); export default router; ``` 这段代码展示了如何利用 `vue-router` 来管理不同页面之间的跳转逻辑[^4]。 --- #### 登录表单设计 下面是一个简单的登录表单模板,它结合了 `el-form` 组件来验证输入数据的有效性: ```html <template> <div class="login-container"> <h2>用户登录</h2> <el-form :model="form" status-icon :rules="rules" ref="ruleFormRef" label-width="80px"> <el-form-item label="用户名" prop="username"> <el-input v-model="form.username"></el-input> </el-form-item> <el-form-item label="密码" prop="password"> <el-input type="password" v-model="form.password" autocomplete="off"></el-input> </el-form-item> <el-form-item> <el-button type="primary" @click="submitForm(ruleFormRef)">提交</el-button> <el-button @click="resetForm(ruleFormRef)">重置</el-button> </el-form-item> </el-form> </div> </template> <script setup lang="ts"> import { reactive, ref } from &#39;vue&#39;; import type { FormInstance } from &#39;element-plus&#39;; const ruleFormRef = ref<FormInstance>(); interface FormData { username: string; password: string; } const form = reactive<FormData>({ username: &#39;&#39;, password: &#39;&#39;, }); const rules = reactive({ username: [{ required: true, message: &#39;请输入用户名&#39;, trigger: &#39;blur&#39; }], password: [{ required: true, message: &#39;请输入密码&#39;, trigger: &#39;blur&#39; }], }); const submitForm = (formEl: FormInstance | undefined) => { if (!formEl) return; formEl.validate((valid) => { if (valid) { console.log(&#39;登录功:&#39;, form); } else { console.error(&#39;校验失败!&#39;); } }); }; const resetForm = (formEl: FormInstance | undefined) => { if (!formEl) return; formEl.resetFields(); }; </script> <style scoped> .login-container { width: 400px; margin: auto; } </style> ``` 该部分实现了基本的表单交互行为,并集了 `Element Plus` 提供的表单项与按钮控件。 --- #### 后端接口调用 假设后端提供了一个 RESTful API 接口 `/api/login` 处理用户的认证请求,则可通过 `axios` 发送 POST 请求完实际的身份验证操作: ```typescript import axios from &#39;axios&#39;; async function login(username: string, password: string): Promise<any> { try { const response = await axios.post(&#39;/api/login&#39;, { username, password }); localStorage.setItem(&#39;token&#39;, response.data.token); // 存储 token 到本地存储 return response.data; } catch (error) { throw new Error(&#39;登录失败,请检查您的账号或网络连接!&#39;); } } ``` 上述函数封装了异步通信流程,便于后续扩展与其他服务集[^2]。 --- #### 数据持久化与状态管理 对于复杂的业务场景,推荐采用 Vuex 或其替代品 Pinia 进行应用级别的状态共享。例如,在用户功登录之后保存令牌信息至 Store 并同步更新界面显示内容。 ```javascript // store/user.ts import { defineStore } from &#39;pinia&#39;; export const useUserStore = defineStore(&#39;user&#39;, { state: () => ({ isLoggedIn: false, token: null as string | null, }), actions: { setToken(token: string) { this.isLoggedIn = true; this.token = token; }, clearToken() { this.isLoggedIn = false; this.token = null; }, }, }); ``` 这样能够更方便地控制整个应用程序的状态流转情况。 --- ### 总结 以上介绍了从环境搭建、UI 构件选用直至前后端联动开发的一整套方案用于构建支持用户身份验证的基础框架结构。希望这些指导可以帮助开发者快速上手实践!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值