Пока я тестировал свой код, я случайно столкнулся с неожиданной мутацией .... Или, может быть ... Я просто делаю все неправильно ...
Пользователь
constructor(
public id: number,
public education: Education[]
){}
UserStateService
private user = new BehaviorSubject<User>(null);
setUser(user:User){
// set by HttpClient or perform an update
this.user.next(user);
}
getUserDetail(){
return this.user.value;
// this.user.getValue(); => tried this as well same thing...
}
updateUserDetail(user:User){
// Maybe perform some check method before the update
this.user.next(user);
// HttpClient to save on db
}
В моем компоненте есть форма, в которой пользователь будет изменять свои данные. Поэтому моя идея заключается в том, чтобы вызвать getUserDetail()
, думая, что возвращаемый объект должен быть только для чтения. Как только я установил новое значение, я бы updateUserDetail()
обновил наблюдаемое с next()
, но я испытал иначе ...
Компонент
onSubmit(){
let currentUser = this.userService.getUserDetail();
console.log("Original User => ", currentUser); // array(2) see screen shot highlighted
currentUser.educations = this.userForm.value['educations'];
console.log("Modify User => ", currentUser); // array(1)
// this.userService.updateUserDetail(currentUser);
}
![enter image description here](https://i.stack.imgur.com/bSvH5.png)
currentUser.educations = this.userForm.value['educations'];
Я не хочу, чтобы это автоматически обновляло наблюдаемое, потому что в некоторых случаях мне может понадобиться проверить информацию, прежде чем вносить изменения ... как я могу этого достичь?
Спасибо