为什么有时候用.key的方式获取不到数据

🧨 问题本质总结:

你发现:

  • 当你用 不带引号写法(如 this.selectedItems.morning)赋值时,最终能成功保存到数据库。
  • 当你用 带引号写法(如 this.selectedItems['morning'])赋值时,最终数据库中是 null

但实际上:

❗️不是“带不带引号”导致的差异,而是你的对象类型定义出了问题。


🔍 你的关键代码如下:

selectedItems: { [timeType: string]: any } = {
  morning: null,
  noon: null,
  night: null
};

这是一个 索引签名对象:意味着你可以通过任意字符串访问属性(比如 selectedItems['xxx']),但它不支持点语法(.)访问未显式声明的字段


✅ 正确理解这个类型的含义

{ [timeType: string]: any }

表示:“这个对象允许你用任意字符串作为 key 来读写属性”,但 TypeScript 并不知道 'morning', 'noon', 'night' 是合法字段!

所以当你写:

this.selectedItems.morning = item.id;

👉 TypeScript 认为 morning 这个字段不存在,它不会把它当作 selectedItems['morning'] 的等价形式处理。

这就导致:

  • 赋值看似成功了(没有报错),但其实只是给对象动态添加了一个新属性,并没有修改原本 selectedItems['morning'] 的值。
  • 所以在后面调用 this.currentEditDevice.set("playJobs", this.selectedItems) 时,虽然对象里有 .morning 属性,但实际 JSON 序列化后变成 {}{ morning: undefined },最后传给后端的就是 null

📌 验证方式(你可以加这段代码测试):

console.log(this.selectedItems); // 看似有 morning 属性
console.log(JSON.stringify(this.selectedItems)); // 实际序列化后 morning 可能是 undefined 或被忽略

✅ 正确解决方案:使用接口或字面量类型

✅ 方法一:使用接口(推荐)

interface SelectedItems {
  morning: any;
  noon: any;
  night: any;
}

selectedItems: SelectedItems = {
  morning: null,
  noon: null,
  night: null
};

这样:

  • 你可以安全地使用 this.selectedItems.morning = xxx
  • JSON.stringify(this.selectedItems) 会正确包含所有字段;
  • 数据也能正常保存到后端。

✅ 方法二:使用类型字面量

selectedItems: { morning: any; noon: any; night: any } = {
  morning: null,
  noon: null,
  night: null
};

效果与接口一样,适合简单结构。


✅ 如果你想保留索引签名 + 支持点语法:

interface SelectedItems {
  morning: any;
  noon: any;
  night: any;
  [key: string]: any; // 允许其他字段
}

这样你既可以写 selectedItems.morning,也可以写 selectedItems[timeType]


✅ 最终建议修复步骤:

  1. selectedItems 类型从索引签名改为具体字段接口:
interface SelectedItems {
  morning: any;
  noon: any;
  night: any;
}

selectedItems: SelectedItems = {
  morning: null,
  noon: null,
  night: null
};
  1. 确保赋值使用点语法或方括号语法都可以:
this.selectedItems.morning = item.id; // ✅ 安全
// 或者
this.selectedItems['morning'] = item.id; // ✅ 同样有效
  1. 打印调试验证:
console.log(this.selectedItems);
console.log(JSON.stringify(this.selectedItems));
  1. 确保数据正确传入设备模型:
this.currentEditDevice.set("playJobs", this.selectedItems);

🎯 总结

问题 原因 解决方法
selectedItems.morning 赋值无效 使用了 { [key: string]: any } 类型,但没显式声明字段 改成 interface SelectedItems 显式定义字段
最终传给后端为空或 null 因为 JSON.stringify() 没有识别 .morning 字段 修改类型定义即可解决

如果你还有关于 TypeScript 类型、Vue 响应式系统、或者 Parse 对象保存的问题,也欢迎继续问我!我可以帮你一步步排查到底 😊

Logo

开源鸿蒙跨平台开发社区汇聚开发者与厂商,共建“一次开发,多端部署”的开源生态,致力于降低跨端开发门槛,推动万物智联创新。

更多推荐