Files
xk-doctor-wx/utils/wxListKey.js
李琦 27202c0740 1. 优化了一些交互,中药药品编辑交互
2. dev和生产环境统一鉴别
2026-05-30 13:53:14 +08:00

24 lines
805 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 微信小程序 v-for :key 仅支持简单属性访问,不支持模板表达式。
* 在 JS 中为列表项预生成 _wxKey 后,模板使用 :key="item._wxKey"
*/
export function withWxKey(list, idField = 'id', fallbackPrefix = 'i') {
return (list || []).map((item, index) => ({
...item,
_wxKey: item[idField] != null && item[idField] !== ''
? String(item[idField])
: `${fallbackPrefix}${index}`,
}))
}
/** 单条消息/列表项补全 _wxKey用于 push、$set 等增量更新) */
export function ensureWxKey(item, idField = 'id', fallback) {
if (!item || typeof item !== 'object') return item
if (item._wxKey != null && item._wxKey !== '') return item
const id = item[idField]
return {
...item,
_wxKey: id != null && id !== '' ? String(id) : String(fallback),
}
}