我想添加一个值到Angular中的现有对象,我从服务调用中获取对象,并在将其保存到本地存储之前,我想保存时间戳。添加键和值在Angular中的现有对象动态
我使用Object.assign
为同一
的问题是,用户类型化与接口,如果我的任何属性添加到它说,该属性没有在接口中定义和接口类不被写入由我的Firebase用户信息。我不想编辑该界面或添加到它。我想动态地创建一个新的属性。
像Java中的反射一样,并在Javascript中添加动态值对象。
这就是我提前已经尝试
this.afAuth.authState.subscribe((data) => {
console.log("Here");
if (data) {
const value = Object.assign(data,{TimeStamp : new Date().getTime()}); // this assign is not working even having obj in variable dosent help
console.log("Value"+JSON.stringify(value));
localStorage.setItem("user", JSON.stringify(value));
this.user = data;
this.service.fetchData(this.user.uid).subscribe((res) => {
this.comments = res;
});
} else {
this.comments = null;
}
});
任何帮助感谢
...没有必要使用分配对象,如果对象存在,只是将它加入对飞'data.newProperty = someValue' – Alex
,而不是'的console.log(“在这里”);'请尝试' console.log(data);'并粘贴输出的问题 – Dhyey
你还没有确定具体的问题...或在这里问了一个问题 – charlietfl