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
    })
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值