- 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: 下拉刷新 / 上拉加载 - 产品首页: 添加业务联系 - 文档: 清理过时文件,补充缺失规范章节
3.6 KiB
3.6 KiB
title, impact, impactDescription, type, tags
| title | impact | impactDescription | type | tags | |||||
|---|---|---|---|---|---|---|---|---|---|
| defineProps Cannot Access Variables from script setup | MEDIUM | Variables declared in script setup are not accessible inside defineProps arguments | gotcha |
|
defineProps Cannot Access Variables from script setup
Impact: MEDIUM - Code inside the defineProps() argument cannot access other variables declared in <script setup>. The entire expression is moved to an outer function scope when compiled, making local variables inaccessible.
This commonly surprises developers trying to use imported constants or computed validation logic.
Task Checklist
- Define validation constants outside
<script setup>or in a separate file - Import constants before using them in defineProps
- Use external type definitions for TypeScript props
- For dynamic validation, use watchers instead of prop validators
Incorrect:
<script setup>
import { ref } from 'vue'
// These are in <script setup> scope
const VALID_SIZES = ['sm', 'md', 'lg']
const maxLength = ref(100)
defineProps({
size: {
type: String,
// WRONG: VALID_SIZES is not accessible here
validator: (v) => VALID_SIZES.includes(v) // ReferenceError!
},
name: {
type: String,
// WRONG: Cannot access refs
validator: (v) => v.length <= maxLength.value // ReferenceError!
}
})
</script>
Correct:
<script>
// Define constants in regular <script> block (module scope)
export const VALID_SIZES = ['sm', 'md', 'lg']
export const MAX_LENGTH = 100
</script>
<script setup>
// Now accessible in defineProps
defineProps({
size: {
type: String,
validator: (v) => VALID_SIZES.includes(v) // Works!
},
name: {
type: String,
validator: (v) => v.length <= MAX_LENGTH // Works!
}
})
</script>
Pattern: Import from External File
// validation.js
export const VALID_SIZES = ['sm', 'md', 'lg']
export const VALID_COLORS = ['red', 'blue', 'green']
export const sizeValidator = (v) => VALID_SIZES.includes(v)
<script setup>
import { VALID_SIZES, VALID_COLORS, sizeValidator } from './validation'
// Imported values ARE accessible
defineProps({
size: {
type: String,
validator: sizeValidator
},
color: {
type: String,
validator: (v) => VALID_COLORS.includes(v)
}
})
</script>
Pattern: Dual Script Blocks
<script>
// Regular script for module-level declarations
const options = {
themes: ['light', 'dark', 'system'],
defaults: {
theme: 'light',
size: 'md'
}
}
</script>
<script setup>
// options is accessible here
const props = defineProps({
theme: {
type: String,
default: options.defaults.theme,
validator: (v) => options.themes.includes(v)
}
})
</script>
TypeScript: External Type Definitions
// types.ts
export interface UserProps {
name: string
email: string
age?: number
}
<script setup lang="ts">
import type { UserProps } from './types'
// Type imports work fine
const props = defineProps<UserProps>()
</script>
Why This Happens
Vue's compiler transforms <script setup> code. The defineProps() call is extracted and moved to component options at compile time, before the setup function runs:
// Your code:
const MY_CONST = 'value'
defineProps({ prop: { default: MY_CONST } })
// Compiled (simplified):
export default {
props: { prop: { default: MY_CONST } }, // MY_CONST doesn't exist here!
setup() {
const MY_CONST = 'value' // Defined too late
}
}