- 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: 下拉刷新 / 上拉加载 - 产品首页: 添加业务联系 - 文档: 清理过时文件,补充缺失规范章节
5.1 KiB
title, impact, impactDescription, type, tags
| title | impact | impactDescription | type | tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| Props Are Read-Only - Never Mutate Props | HIGH | Mutating props breaks one-way data flow and causes unpredictable parent-child state synchronization issues | gotcha |
|
Props Are Read-Only - Never Mutate Props
Impact: HIGH - Props in Vue follow one-way data flow: parent to child only. Mutating a prop in a child component violates this contract, triggering Vue warnings and causing hard-to-debug state synchronization issues. The parent component loses control of the data it passed down.
Props are "snapshots" from the parent at render time. Vue's reactivity system tracks props at the parent level - mutating in the child doesn't notify the parent, leading to state inconsistencies.
This is especially dangerous with object/array props because JavaScript passes them by reference, allowing mutation without assignment.
Task Checklist
- Never assign new values to props
- Never mutate object or array prop properties directly
- Use emit to request parent to make changes
- Create local copies if you need to modify prop-based data
- Use computed properties for derived values
The Problem
Incorrect - Direct primitive prop mutation:
<script setup>
const props = defineProps({
count: Number
})
// WRONG: Vue will warn about mutating props
function increment() {
props.count++ // Mutation attempt - this WILL fail
}
</script>
Incorrect - Object/array prop mutation (silent but dangerous):
<script setup>
const props = defineProps({
user: Object,
items: Array
})
// WRONG: No warning, but breaks data flow!
function updateUser() {
props.user.name = 'New Name' // Mutates parent's object
}
function addItem() {
props.items.push({ id: 1 }) // Mutates parent's array
}
</script>
This pattern is dangerous because:
- Parent component doesn't know about the change
- Data can become out of sync
- Makes debugging difficult - where did the change come from?
- Breaks the component contract
Pattern 1: Emit Events to Parent
Let the parent handle all data changes.
Correct:
<!-- ChildComponent.vue -->
<script setup>
const props = defineProps({
count: Number,
user: Object
})
const emit = defineEmits(['update:count', 'update-user'])
function increment() {
emit('update:count', props.count + 1)
}
function updateName(newName) {
emit('update-user', { ...props.user, name: newName })
}
</script>
<!-- ParentComponent.vue -->
<template>
<ChildComponent
:count="count"
:user="user"
@update:count="count = $event"
@update-user="user = $event"
/>
</template>
Pattern 2: Local Copy for Editable Data (Prop as Initial Value)
When the component needs to work with a modified version of prop data.
Correct:
<script setup>
import { ref, watch } from 'vue'
const props = defineProps({
initialValue: String,
user: Object
})
// Local copy for editing
const localValue = ref(props.initialValue)
// Deep copy for objects
const localUser = ref({ ...props.user })
// Sync when parent changes the prop
watch(() => props.initialValue, (newVal) => {
localValue.value = newVal
})
watch(() => props.user, (newUser) => {
localUser.value = { ...newUser }
}, { deep: true })
</script>
<template>
<input v-model="localValue" />
<input v-model="localUser.name" />
</template>
Pattern 3: Computed Properties for Transformations
When you need a derived/transformed version of the prop.
Correct:
<script setup>
import { computed } from 'vue'
const props = defineProps({
text: String,
items: Array
})
// Derived value - doesn't mutate prop
const uppercaseText = computed(() => props.text.toUpperCase())
// Filtered view - doesn't mutate prop
const activeItems = computed(() =>
props.items.filter(item => item.active)
)
</script>
Pattern 4: v-model for Two-Way Binding
For form-like components that need two-way binding.
Correct:
<!-- CustomInput.vue -->
<script setup>
const props = defineProps({
modelValue: String
})
const emit = defineEmits(['update:modelValue'])
</script>
<template>
<input
:value="modelValue"
@input="emit('update:modelValue', $event.target.value)"
/>
</template>
<!-- ParentComponent.vue -->
<template>
<!-- v-model is shorthand for :modelValue + @update:modelValue -->
<CustomInput v-model="text" />
</template>
Common Mistake: Thinking Object Mutation Is Safe
<script setup>
const props = defineProps({ config: Object })
// This "works" but is an anti-pattern!
props.config.theme = 'dark' // No Vue warning, but still wrong
</script>
Vue doesn't warn because it can't efficiently detect deep mutations. But this still:
- Breaks one-way data flow
- Makes the component unpredictable
- Causes maintenance nightmares
Always treat props as if they were deeply frozen.