rxjs에서는 복잡한 객체의 속성을 변경하고 싶지만 다른 것은 변경하지 않습니다.
예를 들면 :
사용자 클래스가 있습니다.
class User {
constructor(
public name: string,
public role: string
) { }
}
내 서비스 :
@Injectable()
export class AuthService {
user: BehaviorSubject<User>;
constructor() {
this.user = new BehaviorSubject(
new User('wizardnet', 'admin')
);
}
setRole(role) {
// change ONLY the Role property of this.user ???????
// subscribe then clone the object and then call next() ??
}
}
그렇게하는 방법?
실제로 서비스에서 사용자의 가치를 얻기 위해 구독 할 필요가 없다는 점을 제외하면 모든 것이 정확합니다. 다음을 사용할 수 있습니다 .value
.
@Injectable()
export class AuthService {
user: BehaviorSubject<User>;
constructor() {
this.user = new BehaviorSubject(
new User('wizardnet', 'admin')
);
}
setRole(role) {
const updateUser = Object.assign(this.user.value, {role: role});
this.user.next(updateUser);
return this.user.asObservable();
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다