基于vue3+ts+vant4封装Hook函数

基于vue3+ts+vant4封装composable as Hook

自我记录

提取一个hook提供,查看图片预览函数
xxx使用,xx中也使用
现在是只有一个函数复用,其实也可以复用状态数据之类的,或者多个函数。

src/composable/index.ts

import {
    
     showImagePreview } from 'vant'
import {
    
     ref } from 'vue'
// 封装查看预览图片逻辑
// 封装逻辑,规范 useXxx,表示使用某功能
export const useShowPrescription = () => {
    
    
  // 预览图片函数
  const showPrescription = async (id?: string) => {
    
    
    if (id) {
    
    
      const res = await getPrescriptionPic(id)
      showImagePreview([res.data.url])
    }
  }
  return {
    
     showPrescription }
}

使用src/views/User/index.vue

<script setup lang="ts">
import {
    
     useShowPrescription } from '@/composable'
const {
    
     showPrescription } = useShowPrescription()
const onSelect = (i: number) => {
    
    
  if (i === 0) {
    
    
    showPrescription(urlId)
  }
}
</script>

猜你喜欢

转载自blog.csdn.net/zhgweb/article/details/130729680
今日推荐