- 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.0 KiB
title, impact, impactDescription, type, tags
| title | impact | impactDescription | type | tags | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Always Declare Emits for Documentation and Validation | MEDIUM | Undeclared emits cause warnings, break TypeScript inference, and prevent event validation | best-practice |
|
Always Declare Emits for Documentation and Validation
Impact: MEDIUM - Declaring emitted events with defineEmits() or the emits option is technically optional, but strongly recommended. Without declarations, Vue shows runtime warnings, TypeScript can't infer event types, and you lose the ability to validate event payloads.
Declared emits also serve as self-documentation, making it immediately clear what events a component can emit.
Task Checklist
- Use
defineEmits()in<script setup>to declare all events - Use
emitsoption when not using<script setup> - Add TypeScript types for event payloads
- Consider adding validation functions for complex payloads
- Document the purpose of each event
The Warning
When you emit without declaring:
<script setup>
// No defineEmits declaration
function handleClick() {
emit('select', item) // Vue warns in dev mode
}
</script>
Vue warns:
[Vue warn]: Component emitted event "select" but it is neither declared
in the emits option nor as an "onSelect" prop.
Basic Declaration
Correct - Array syntax:
<script setup>
const emit = defineEmits(['submit', 'cancel', 'update'])
function handleSubmit() {
emit('submit', formData)
}
function handleCancel() {
emit('cancel')
}
</script>
Correct - Options API:
export default {
emits: ['submit', 'cancel', 'update'],
methods: {
handleSubmit() {
this.$emit('submit', this.formData)
}
}
}
TypeScript Typed Emits
Correct - Type-based declaration (recommended for TypeScript):
<script setup lang="ts">
interface User {
id: number
name: string
}
const emit = defineEmits<{
submit: [data: FormData]
cancel: []
'update:modelValue': [value: string]
select: [user: User, index: number]
}>()
// Now TypeScript enforces correct payloads
emit('submit', formData) // OK
emit('submit') // Error: Expected 1 argument
emit('select', user) // Error: Expected 2 arguments
emit('unknown') // Error: Unknown event
</script>
Alternative syntax (Vue 3.3+):
<script setup lang="ts">
const emit = defineEmits<{
(e: 'submit', data: FormData): void
(e: 'cancel'): void
(e: 'update:modelValue', value: string): void
}>()
</script>
Event Validation
You can validate event payloads at runtime:
Correct - Validation functions:
<script setup>
const emit = defineEmits({
// No validation, just declaration
cancel: null,
// Validate payload
submit: (payload) => {
if (!payload.email) {
console.warn('Submit event requires email')
return false
}
return true
},
// Validate with type checking
click: (id) => typeof id === 'number'
})
</script>
Returning false from a validator logs a console warning but doesn't prevent the event from being emitted.
Benefits of Declaring Emits
1. Fallthrough Attribute Separation
Without declaration, native event listeners fall through to the root element:
<!-- ParentComponent.vue -->
<ChildComponent @click="handleClick" />
<!-- ChildComponent.vue - WITHOUT emits declaration -->
<template>
<!-- Native click listener falls through to button -->
<button>Click me</button>
</template>
With declaration, Vue knows it's a component event:
<script setup>
// Now Vue knows 'click' is a component event, not native
const emit = defineEmits(['click'])
</script>
2. Self-Documentation
<script setup>
// Clear contract: this component emits these events
const emit = defineEmits<{
'row-click': [row: TableRow]
'row-select': [row: TableRow, selected: boolean]
'page-change': [page: number]
'sort-change': [column: string, direction: 'asc' | 'desc']
}>()
</script>
3. IDE Support
With declarations, your IDE can:
- Autocomplete event names when using the component
- Show event payload types
- Warn about typos in event names
- Navigate to event definitions
$emit in Template vs emit in Script
<script setup>
// $emit is available in template, but...
// emit() is needed in <script setup>
const emit = defineEmits(['submit'])
function handleSubmit() {
// $emit doesn't work here - use emit()
emit('submit', data)
}
</script>
<template>
<!-- $emit works in template -->
<button @click="$emit('submit', data)">Submit</button>
<!-- Or use the declared emit function -->
<button @click="emit('submit', data)">Submit</button>
</template>