本文主要是介绍封装hook函数【便于复用】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
目录
- 一般函数封装
- 封装hook函数
一般函数封装
普通的删除逻辑封装函数—子组件点击删除-通过 defineEmits 通知父组件(自定义事件)进行删除
const deleteLoading = ref(false)
const emits = defineEmits<{(e: 'click-delete', id: string): void
}>()
const deleteConsultOrder = async (item: ConsultOrderItem) => {try {deleteLoading.value = trueawait deleteOrder(item.id)showSuccessToast('删除成功')emits('click-delete', item.id)} catch (error) {showFailToast('删除失败')} finally {deleteLoading.value = false}
}
父组件ConsultItem.vue
<div class="foot" v-if="item.status === OrderType.ConsultComplete"><consult-more :disabled="!item.prescriptionId" @on-delete="deleteConsultOrder(item)"@on-preview="onShowPrescription(item.prescriptionId)"></consult-more>
</div>
子组件ConsultMore.vue
<template><div class="more"><van-popover placement="top-start" v-model:show="showPopover" :actions="actions" @select="onSelect"><template #reference> 更多 </template> </van-popover></div>
</template><script setup lang="ts">
import { computed, ref } from 'vue'
import { showConfirmDialog } from 'vant'const props = defineProps<{disabled: boolean
}>()
const showPopover = ref(false)
const actions = computed(() => [{ text: '查看处方', disabled: props.disabled },{ text: '删除订单' },
])const emit = defineEmits<{(e: 'on-delete'): void(e: 'on-preview'): void
}>()const onSelect = (actions: { text: string }, index: number) => {console.log(index, 9090);if (index === 0) {emit('on-preview')}if (index === 1) {emit('on-delete')}
};
</script>
封装hook函数
src/composable/index.ts
①封装hook函数
模板:
// 封装逻辑,规范 useXxx,表示使用某功能
export const useFollow = (cb: () => void) => {
const xxx = () => {}
return { xxx }
}
②调用hook函数
模板:
const emits = defineEmits<{
(e: 'click-delete', id: string): void
}>()
const { xxx } = usexxx(() => {
emits('click-delete', props.item.id)
})
import { ref } from 'vue'
import { followOrUnfollow, getPrescriptionOrder, cancelOrder, deleteOrder } from '@/services/consult'
import type { FollowType, ConsultOrderItem } from '@/types/consult'
import { showImagePreview, showFailToast, showSuccessToast } from 'vant';
import { OrderType } from '@/enums'
import { defineEmits } from 'vue'// 封装逻辑,规范 useXxx,表示使用某功能
export const useFollow = (type: FollowType = 'doc') => {const loading = ref(false)// {a, b} 类型,传值得时候 {a, b, c} 也可以,这是类型兼容:多的可以给少的const follow = async (item: { id: string; likeFlag: 0 | 1 }) => {loading.value = truetry {await followOrUnfollow(item.id, type)item.likeFlag = item.likeFlag === 1 ? 0 : 1} finally {loading.value = false}}return { loading, follow }
}// 封装查看处方hook函数
export const useShowPrescription = () => {const onShowPrescription = async (id: string) => {const res = await getPrescriptionOrder(id)console.log(res.data.url);// 预览处方showImagePreview([res.data.url])}return { onShowPrescription }
}// 封装取消问诊hook函数
export const useCancelConsultOrder = () => {const loading = ref(false)const cancelConsultOrder = async (item: ConsultOrderItem) => {try {loading.value = trueconst res = await cancelOrder(item.id)console.log(res, '取消问诊');item.status = OrderType.ConsultCancelitem.statusValue = '已取消'showSuccessToast('取消成功')} catch (error) {showFailToast('取消失败')} finally {loading.value = false}}return { loading, cancelConsultOrder }
}// 封装删除问诊hook函数
export const useDeleteConsultOrder = (cb: () => void) => {const deleteLoading = ref(false)const deleteConsultOrder = async (item: ConsultOrderItem) => {try {deleteLoading.value = trueawait deleteOrder(item.id)showSuccessToast('删除成功')// 成功,做其他业务cb && cb()} catch (error) {showFailToast('删除失败')} finally {deleteLoading.value = false}}return { deleteLoading, deleteConsultOrder }
}
User/components/ConsultItem.vue
import { useShowPrescription, useCancelConsultOrder, useDeleteConsultOrder } from '@/composable'
const { onShowPrescription } = useShowPrescription()
const { loading, cancelConsultOrder } = useCancelConsultOrder()const emits = defineEmits<{(e: 'click-delete', id: string): void
}>()
const { deleteLoading, deleteConsultOrder } = useDeleteConsultOrder(() => {emits('click-delete', props.item.id)
})<!--//样式 -->
<div class="foot" v-if="item.status === OrderType.ConsultComplete"><consult-more :disabled="!item.prescriptionId" @on-delete="deleteConsultOrder(item)"@on-preview="onShowPrescription(item.prescriptionId)"></consult-more>
</div>
这篇关于封装hook函数【便于复用】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!