- 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: 下拉刷新 / 上拉加载 - 产品首页: 添加业务联系 - 文档: 清理过时文件,补充缺失规范章节
4.8 KiB
4.8 KiB
title, impact, impactDescription, type, tags
| title | impact | impactDescription | type | tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| Vue Composition API Runs Once, Unlike React Hooks | MEDIUM | Understanding this difference prevents over-engineering and React patterns that don't apply | gotcha |
|
Vue Composition API Runs Once, Unlike React Hooks
Impact: MEDIUM - Vue's setup() or <script setup> executes only once per component instance, while React Hooks run on every render. Developers coming from React often apply patterns (dependency arrays, excessive memoization, useCallback) that are unnecessary and counterproductive in Vue.
Understanding this fundamental difference is crucial for writing idiomatic Vue code. Vue's approach eliminates entire categories of bugs (stale closures, exhaustive deps) that plague React applications.
Task Checklist
- Don't implement "dependency arrays" - Vue tracks dependencies automatically
- Don't wrap functions in "useCallback" equivalents - not needed in Vue
- Don't use "useMemo" patterns - Vue's
computed()handles this automatically - Understand that closures in Vue don't go "stale" like in React
- Don't worry about "call order" - Vue composables can be conditional
React Patterns to Avoid in Vue:
// These patterns are UNNECESSARY in Vue - they solve React-specific problems
// WRONG: Trying to implement dependency arrays (React pattern)
watch(
[dep1, dep2, dep3], // Vue tracks deps automatically in watchEffect
() => {
// ...
}
)
// Unless you specifically WANT to control which deps trigger the watcher,
// prefer watchEffect() which auto-tracks
// WRONG: Memoizing callbacks like useCallback
const memoizedHandler = computed(() => {
return () => doSomething(state.value)
})
// In Vue, just define the function normally - no memoization needed
// WRONG: Worrying about stale closures
function useData() {
const data = ref(null)
// In React, this could capture stale 'data' - NOT in Vue!
// Vue refs are always current
const handler = () => {
console.log(data.value) // Always gets current value
}
return { data, handler }
}
Correct Vue Patterns:
import { ref, computed, watchEffect } from 'vue'
// CORRECT: Auto-dependency tracking with watchEffect
const query = ref('')
const filter = ref('all')
watchEffect(() => {
// Vue automatically detects that this depends on query and filter
// No dependency array needed!
fetchResults(query.value, filter.value)
})
// CORRECT: computed() handles memoization automatically
const expensiveResult = computed(() => {
// Only recalculates when dependencies actually change
return heavyComputation(data.value)
})
// CORRECT: Functions don't need memoization
function handleClick() {
count.value++
}
// Just use it directly - no useCallback wrapper needed
// <button @click="handleClick">
// CORRECT: Closures always access current values
const count = ref(0)
const message = ref('')
function logState() {
// This always logs CURRENT values, never stale ones
console.log(`Count: ${count.value}, Message: ${message.value}`)
}
setTimeout(() => {
logState() // Gets current values even if called later
}, 5000)
Vue's Advantages Over React Hooks
// 1. No stale closure problems
const count = ref(0)
onMounted(() => {
setInterval(() => {
// In React: would need useRef or deps array to avoid stale value
// In Vue: count.value is always current
console.log(count.value)
}, 1000)
})
// 2. Composables can be conditional
if (featureEnabled) {
const { data } = useSomeFeature() // This is FINE in Vue!
}
// In React: "Hooks cannot be conditional" - not a problem in Vue
// 3. No exhaustive-deps linting headaches
watchEffect(() => {
// Use any reactive values - Vue tracks them all automatically
// No ESLint rule yelling about missing dependencies
doSomething(a.value, b.value, c.value)
})
// 4. Child components don't need memoization by default
// Vue's reactivity system only updates what actually changed
// No need for React.memo() equivalents in most cases
When Vue Patterns Differ
// Setup runs once - so initialization happens once
<script setup>
import { ref, onMounted } from 'vue'
// This code runs ONCE when component is created
const data = ref(null)
console.log('Setup running') // Only logs once
onMounted(() => {
console.log('Mounted') // Only logs once
})
// If you need something to run on every reactive change,
// use watch or watchEffect
watchEffect(() => {
// This runs when dependencies change
console.log('Data changed:', data.value)
})
</script>