12451 b553c4e519 feat: API 统一 + 设备详情补全 + H5 导航优化
- API 路由统一: 移除 /api/h5/,统一 /api/pms/ 入口
- 二进制改名: api-server -> pms-api(与服务名一致)
- nginx: 合并 /api/ + /pms/api/ 为 /api/pms/
- 新增 docs/API.md 接口文档
- 后端: CabinetDetail 增加 rssi/pow_fail/address/soc 等字段
- Redis 管道批量读取仓体实时数据(SOC/电压/电流/功率)
- DB: 所有 TIMESTAMP 列改为 DATETIME(修复类型不匹配)
- 用户管理: 新增创建时间/修改时间列,状态改用 Switch
- 角色管理/用户管理: 操作按钮改为图标
- PC 设备详情: 基本信息补全 RSSI/ICCID/电源/地址
- PC 卡片: 通道统计改用 Lucide 图标,颜色统一
- PC 二维码: canvas 渲染,含编号文字,支持右键下载
- H5: 按 uni-app 规范修正导航(navigateTo 替代 reLaunch)
- H5: 详情页加返回导航条
- H5: 扫码支持摄像头(jsQR)+ ?id= 参数
- H5: 底部 tabBar 在所有页面常显
- H5: 首页统计改为 6 项(设备/通道/空闲/充电/充满/故障)
- H5: 下拉刷新 / 上拉加载
- 产品首页: 添加业务联系
- 文档: 清理过时文件,补充缺失规范章节
2026-07-04 04:03:02 +08:00

3.6 KiB

title, impact, impactDescription, type, tags
title impact impactDescription type tags
Choose v-if or v-show Based on Toggle Frequency MEDIUM Wrong choice causes unnecessary DOM operations or wasted initial render cost capability
vue3
conditional-rendering
v-if
v-show
performance

Choose v-if or v-show Based on Toggle Frequency

Impact: MEDIUM - Using the wrong directive for your use case leads to suboptimal performance. v-if has higher toggle costs (destroys/recreates elements), while v-show has higher initial render costs (always in DOM). Choosing incorrectly can cause unnecessary DOM manipulation or waste memory on hidden elements.

v-if is "real" conditional rendering that properly destroys and recreates event listeners and child components. v-show just toggles CSS display property, keeping everything in the DOM.

Task Checklist

  • Use v-show for elements that toggle frequently (modals, dropdowns, panels)
  • Use v-if for conditions that rarely change at runtime (auth states, feature flags)
  • Use v-if when initial condition is likely false (lazy rendering saves cost)
  • Use v-if when child components have expensive setup/teardown
  • Consider the memory cost of keeping hidden elements in DOM with v-show

Incorrect:

<!-- WRONG: Frequent toggle with v-if causes expensive DOM operations -->
<template>
  <button @click="showPanel = !showPanel">Toggle</button>

  <!-- User clicks this frequently - v-if destroys/recreates every time -->
  <div v-if="showPanel" class="settings-panel">
    <ComplexForm />
  </div>
</template>
<!-- WRONG: Rarely-true condition with v-show wastes initial render -->
<template>
  <!-- Admin panel shown only for admin users (rare), but v-show renders it for everyone -->
  <AdminPanel v-show="user.isAdmin" />
</template>

Correct:

<!-- CORRECT: Frequent toggle with v-show - cheap CSS toggle -->
<template>
  <button @click="showPanel = !showPanel">Toggle</button>

  <!-- v-show just toggles display: none, no DOM destruction -->
  <div v-show="showPanel" class="settings-panel">
    <ComplexForm />
  </div>
</template>
<!-- CORRECT: Rare condition with v-if - lazy rendering -->
<template>
  <!-- Only rendered for admins, non-admins don't pay the render cost -->
  <AdminPanel v-if="user.isAdmin" />
</template>
<!-- CORRECT: Auth-gated content with v-if -->
<template>
  <!-- v-if is ideal here - auth state rarely changes -->
  <div v-if="isAuthenticated">
    <UserDashboard />
  </div>
  <div v-else>
    <LoginPrompt />
  </div>
</template>

Decision Guide

Scenario Recommendation Reason
Toggle frequently (tabs, accordions, dropdowns) v-show Cheap CSS toggle
Toggle rarely (auth, feature flags) v-if Lazy render, clean DOM
Initially false, may never be true v-if Never pays render cost
Complex child components with state v-if Properly resets state on toggle
Need to preserve component state across toggles v-show Component stays alive

Key Differences

// v-if behavior:
// - Condition false: Element NOT in DOM, child components NOT created
// - Condition true: Element added, child components created, mounted hooks fire
// - Condition false again: Element removed, child components destroyed, unmounted hooks fire

// v-show behavior:
// - Condition false: Element IN DOM with display: none, child components exist
// - Condition true: display: none removed
// - Component lifecycle hooks only fire once (on initial mount)

Reference