当前位置:网站首页 > Vue.js开发 > 正文

vue3封装表单组件(一)之el-select & `${}之接口抽离公共词根

vue3封装表单组件(一)之el-select & `${}之接口抽离公共词根

效果

在这里插入图片描述

方式一:常规写法

index.vue

<template> <div class="about"> <el-form ref="ruleFormRef" :model="ruleForm" status-icon label-width="120px" class="demo-ruleForm"> <el-form-item label="审批状态" prop="examineState"> <el-select v-model="ruleForm.processState" placeholder="请选择"> <el-option label="通过" value="1" /> <el-option label="驳回" value="2" /> </el-select> </el-form-item> </el-form> </div> </template> <script lang="ts" setup> import { reactive, ref } from 'vue' const ruleFormRef = ref(null) const ruleForm = reactive({ examineState: '' }) </script> <style scoped> .about { width: 500px; } </style> 
方式二:封装写法
1、页面

index.vue

<template> <div class="about"> <el-form ref="ruleFormRef" :model="ruleForm" status-icon label-width="120px" class="demo-ruleForm"> <el-form-item label="审批状态" prop="examineState"> <!-- <el-select v-model="ruleForm.processState" placeholder="请选择"> <el-option label="通过" value="1" /> <el-option label="驳回" value="2" /> </el-select> --> <dictSelect v-model="ruleForm.examineState" :dictid="199" /> </el-form-item> </el-form> </div> </template> <script lang="ts" setup> import { reactive, ref } from 'vue' import dictSelect from '@/components/dictSelect' const ruleFormRef = ref(null) const ruleForm = reactive({ examineState: '' }) </script> <style scoped> .about { width: 500px; } </style> 

引用组件

2、组件

src\components\dictSelect.vue

<template> <!-- class="m-2" --> <el-select v-model="dictValue" placeholder="请选择" @change="selectDictEvent" size="default"> <el-option v-for="item in dictData" :key="item.paraCode" :label="item.paraDesc" :value="item.paraCode"> </el-option> </el-select> </template> <script lang="ts" setup> // import { queryId } from '@/api/couny' import { ref, toRef } from 'vue' const props = defineProps({ modelValue: { default: null, type: String }, dictid: { default: null, type: Number } }) const emit = defineEmits(['update:modelValue']) const dictData = ref([ { paraDesc: '通过', paraCode: '1', }, { paraDesc: '驳回', paraCode: '2', }, ]) const queryDictList = () => { console.log( props.dictid); // queryId({ id: props.dictid }).then((res: any) => { // dictData.value = res.data // }) } queryDictList() const dictValue = toRef(props, 'modelValue') const selectDictEvent = (val: any) => { emit('update:modelValue', val) } </script> 

引用接口

3、接口

src\api\couny.js

import request from '@/utils/request' const url = '/thingsgrid-science' //枚举值查询--写法一 export const queryId = (id)=>{ 
    return request({ 
    url:`${ 
     url}/dict/queryDictById`, method:'post', data }) } // 获取表格数据 -- 写法二 export function getList(params){ 
    return request({ 
    url:'api/thingsgrid-system/dept/lazy-list', method:'get', params, }) } // 删除数据 -- 写法三 export function remove (params){ 
    return request({ 
    url:'api/thingsgrid-system/dept/remove', method:'post', params, }) } // 牵头单位组织树列表 export const getUnits = (data)=>{ 
    return request({ 
    url:`${ 
     url}/unit/getUnits`, method:'post', data }) } 
到此这篇vue3封装表单组件(一)之el-select & `${}之接口抽离公共词根的文章就介绍到这了,更多相关内容请继续浏览下面的相关推荐文章,希望大家都能在编程的领域有一番成就!

版权声明


相关文章:

  • vue3封装表单组件(二)01之el-tree-select & vue3封装组件写法2024-11-30 09:27:09
  • vue3封装表单组件(三)01封装el-dialog组件的传值-radio单选框、form表单之defineEmits、defineProps的使用2024-11-30 09:27:09
  • vue3封装表单组件(三)02封装el-dialog组件的传值-select选择框、input输入框之查询、重置的使用 & vue3中父子传值的使用步骤2024-11-30 09:27:09
  • vue3中,封装el-select选择器组件(一)——表单四等分布局、查询、重置resetFields、el-date-picker起止日期、特殊字符校验、自定义指令v-throttle用于按钮防抖2024-11-30 09:27:09
  • vue3中,封装el-select选择器组件(二)——filterable模糊搜索、watch用法、nextTick用法、toRefs用法、sessionStorage取值、设置基准地址apiUrl2024-11-30 09:27:09
  • axios配置多个接口请求(四)——vue项目axios配置多个IP地址,并配置多个接口请求2024-11-30 09:27:09
  • axios配置多个接口请求(三)——vue项目axios配置多个IP地址,并配置多个接口请求2024-11-30 09:27:09
  • axios配置多个接口请求(一)——vue项目axios配置多个接口请求2024-11-30 09:27:09
  • vue2中,promise.all调用接口的用法2024-11-30 09:27:09
  • vue3进阶(三)——el-tabs标签页组件的使用& 设置sessionStorage.setItem()、获取sessionStorage.getItem()& router.push()跳转页面2024-11-30 09:27:09
  • 全屏图片