简体   繁体   English

Rxjs行为主体未使用Ionic 4和Angle 7更新UI上的数据

[英]Rxjs Behavior subject not updating data on the UI using ionic 4 and angular 7

I have an array of type BehaviourSubject defined and initialized in service and it gets updated upon editing it using another function. 我在服务中定义并初始化了一个类型为BehaviourSubject的数组,并在使用另一个函数对其进行编辑后对其进行了更新。 However, the results displayed on the UI are not of the new edited array but the changes are successfully applied to the external database on a server. 但是,UI上显示的结果不是新编辑的数组的结果,但是更改已成功应用于服务器上的外部数据库。

I have tried many online solutions but still, it won't work. 我已经尝试了许多在线解决方案,但是仍然无法使用。

Here is the code for the Array initialization in the service named 'ServicesService'. 这是名为“ ServicesService”的服务中的数组初始化的代码。

private _nationalProps: BehaviorSubject<NationalProp[]> = new BehaviorSubject<NationalProp[]>([]);

Here is the function in the service to access the array as an observable; 这是服务中以可观察方式访问数组的函数;

get national() {
    return this._nationalProps.pipe(take(1), map(nprops => {
      return [...nprops];
    })
    );

} }

Here is the function in the service for deleting one element in the array. 这是服务中用于删除数组中一个元素的函数。 First, it deletes it on the server than on the local array. 首先,它在服务器上而不是在本地阵列上将其删除。

deleteNationalProp(id: string) {
    return this.http.delete(this.serverUrl + `/national_props/${id}.json`)
          .pipe(
          switchMap(() => {
            return this._nationalProps;
          }),
          take(1),
          tap(props => {
      this._nationalProps.next(props.filter(b => b.id !== id));
    }));
  }

This is the code in another component where I subscribe to the BehaviourSubject to keep track of any changes; 这是我订阅BehaviourSubject来跟踪任何更改的另一个组件中的代码;

propSub: Subscription; // imported from rxjs
loadedProps: NationalProp[];

ngOnInit() {
 this.propSub = this.servicesSrv.national.subscribe(props => {
      this.loadedProps = props;
    });

I call the delete method in the service to remove one element using this function; 我使用该函数在服务中调用delete方法以删除一个元素;

this.servicesSrv.deleteNationalProp(pId).subscribe();

What I expected was that once I delete an element from the array it should also be removed from the list on the UI without having to refresh the page or revisit the page but this does not seem to work. 我期望的是,一旦我从数组中删除了元素,也应该将其从UI列表中删除,而不必刷新页面或重新访问页面,但这似乎不起作用。

I will appreciate your help on how to solve this. 感谢您在解决此问题方面的帮助。

Try after removing take(1) from national property like this: 删除后尝试take(1)national性质是这样的:

get national() {
    return this._nationalProps.pipe(map(nprops => {
      return [...nprops];
    })
    );
}

I managed to solve my own problem. 我设法解决了自己的问题。 Anyone with similar issue can try my solution below. 任何有类似问题的人都可以在下面尝试我的解决方案。

I had to subscribe to the observable in the ionViewWillEnter() life cycle hook instead of ngOnInit() 我必须订阅ionViewWillEnter()生命周期挂钩中的observable而不是ngOnInit()

ie Change subscription from here: 即从此处更改订阅:

ngOnInit() {
 this.propSub = this.servicesSrv.national.subscribe(props => {
      this.loadedProps = props;
    });

To here: 到这里:

ionViewWillEnter() {
 this.propSub = this.servicesSrv.national.subscribe(props => {
      this.loadedProps = props;
    });

Also to unsubscribe I had to do it in the ionViewWillLeave and not ngOnDestroy. 另外,要取消订阅,我必须在ionViewWillLeave而不是ngOnDestroy中进行。

ie Change unsubscribe from here: 即从此处更改退订:

ngOnDestroy() {
    if (this.proposalsSub) {
      this.proposalsSub.unsubscribe();
    }
  }

to here: 到这里:

ionViewWillLeave() {
    if (this.proposalsSub) {
      this.proposalsSub.unsubscribe();
    }
  }

Also I changed the function in the service to return as observable. 我也更改了服务中的功能以使其可观察返回。

ie Change from this: 即从此更改:

get national() {
    return this._nationalProps.pipe(take(1), map(nprops => {
      return [...nprops];
    })
    );

To this: 对此:

get national() {
    return this._nationalProps.asObservable();
    );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM