如何从BehaviourSubject读取数据并向其中发出新数据

问题描述

我有一个BehavIoUrSubject,其中包含一组员工数据。当我想向数组中添加新员工时,我想获取旧数据,在其中插入新记录,然后将其重新放入行为主体。我不知道如何做到这一点。

String str = string.replaceAll("no","");

我正在尝试类似于添加员工方法但可以解决方法

在StackBlitz上也可以找到相同的代码

https://stackblitz.com/edit/angular-buwj6n

解决方法

我不确定我是否正确回答了您的问题...

但是您是否尝试过使用Subject.value?

 export class AppComponent implements OnInit {
 response;

 $subject: BehaviorSubject<any[]> = new BehaviorSubject<any[]>([]);

 constructor(private http: HttpClient) {
  this.$subject.subscribe(res => console.log(res));
 }

 ngOnInit(): void {
   this.http.get<{data: []}> ('http://dummy.restapiexample.com/api/v1/employees')
    .subscribe(res => this.$subject.next(res.data));
 }

 onSubmit() {
  this.$subject.value.push(
   {id: 25,employee_name: 'Sys',employee_salary: 85600,employee_age: 23,profile_image: ''}
  );
  this.$subject.next(this.$subject.value);
 }
}