vue解构平铺VS对象包裹

vue解构平铺VS对象包裹

composable(useXxx):函数内部存在 ref/reactive Vue 响应式 API,返回一个对象,把所有状态和方法打包进去,方便统一接收。

解构平铺

复制代码
export function useTableSearch(initQuery, apiFun) {
  const queryParams = reactive({ ...initQuery })
  const tableData = ref([])
  const total = ref(0)

  const getList = async () => {
    const res = await apiFun(queryParams)
    tableData.value = res.data.records
    total.value = res.data.total
  }

  const resetQuery = () => {
    Object.assign(queryParams, initQuery)
    queryParams.pageNum = 1
    getList()
  }

  return {
    queryParams,
    tableData,
    total,
    getList,
    resetQuery
  }
}

组件使用:

复制代码
const { queryParams, tableData, total, getList, resetQuery } = useTableSearch({}, getListApi)

对象包裹

复制代码
import { reactive, ref } from 'vue'

export function useTableSearch(initQuery, apiFun) {
  const queryParams = reactive({ ...initQuery })
  const tableData = ref([])
  const total = ref(0)

  const getList = async () => {
    const res = await apiFun(queryParams)
    tableData.value = res.data.records
    total.value = res.data.total
  }

  const resetQuery = () => {
    Object.assign(queryParams, initQuery)
    queryParams.pageNum = 1
    getList()
  }

  // ✅ 全部打包到 table 对象里面返回
  const table = {
    queryParams,
    tableData,
    total,
    getList,
    resetQuery
  }

  return { table }
}

组件调用方式

复制代码
const { table } = useTableSearch({}, getListApi)

// 使用
table.queryParams.pageNum = 1
table.getList()
table.resetQuery()

为什么 composable 适合对象包裹,而 helper/utils 是另一种形态

  1. composables/useXxx.js (useTableSearch)
    内部有 ref/reactive,返回一个包裹对象 。
    好处:
  • 导入简单:只解构拿到 table,不用一堆变量
  • 溯源:table.getList(),一眼知道是表格相关方法
  • 多个 composable 共存时不会变量命名冲突(页面同时有 userTable、deptTable)
  1. helper/xxxHelper.js
    直接export default导出对象,toastHelper.showSuccess()
  2. utils/xxxUtil.js
    直接export default导出对象,treeUtil.arrayToTree()

三层规范汇总:

  • composable:export function useXxx(){ return { xxx } }
  • helper:const xxxHelper = {}; export default xxxHelper
  • utils:const xxxUtil = {}; export default xxxUtil

补充优化(加上 try catch 和组件卸载标记,和你之前 useTableQuery 保持一致)

复制代码
import { reactive, ref, onUnmounted } from 'vue'

export function useTableSearch(initQuery, apiFun) {
  const queryParams = reactive({ ...initQuery })
  const tableData = ref([])
  const total = ref(0)
  let isUnmount = false

  const getList = async () => {
    try {
      const res = await apiFun(queryParams)
      if (isUnmount) return
      tableData.value = res.data.records
      total.value = res.data.total
    } catch (err) {
      console.error('查询表格失败:', err)
    }
  }

  const resetQuery = () => {
    Object.assign(queryParams, initQuery)
    queryParams.pageNum = 1
    getList()
  }

  onUnmounted(() => {
    isUnmount = true
  })

  const table = {
    queryParams,
    tableData,
    total,
    getList,
    resetQuery
  }

  return { table }
}

重点区分:

✅ composable 是函数执行后返回包裹对象

✅ helper/utils 是直接导出静态对象

相关推荐
qq_274475151 小时前
第 28 篇:Promise 全解析:状态、then/catch/finally、静态方法 all/race/allSettled/any
开发语言·javascript
GuWenyue3 小时前
昵称限 20 字,8 个 emoji 却超长:一个「字」在系统里有四种长度
javascript·数据库·后端
CV-deeplearning3 小时前
Chrome DevTools实战
前端·agent·chrome devtools·mcp
xurileidian3 小时前
第 30 篇 · 边玩边学Python · 猜数字小游戏 把判断循环异常串起来
前端·python·算法
前端精髓3 小时前
三种方式实现 Agent 工具调用:OpenAI、LangChain 与 LangGraph
前端·数据库·langchain
国科安芯4 小时前
光链路捕获的那一刻:商业激光通信终端捕获跟踪伺服与探测前端的低噪声供电
前端·网络·卫星·电源芯片·抗辐照芯片·在轨
一铭111994 小时前
微信小程序可视化搭建实战:模板、组件与发布验收
前端·低代码·微信小程序·小程序·页面设计·可视化搭建·发布验收
LabVIEW开发4 小时前
LabVIEW 2025 新特性盘点:这几个改进,值得你认真看一遍
前端·labview
画中有画6 小时前
自动化脚本中使用RSA
前端·javascript·自动化
要吃这碗饭7 小时前
Codex failed to revert changes怎么办?2026年4类成因与恢复指南
前端·人工智能