Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular rxjs behaviorsubject未更改Ionic 4视图_Angular_Ionic Framework_Rxjs_Ionic4 - Fatal编程技术网

Angular rxjs behaviorsubject未更改Ionic 4视图

Angular rxjs behaviorsubject未更改Ionic 4视图,angular,ionic-framework,rxjs,ionic4,Angular,Ionic Framework,Rxjs,Ionic4,我使用Ionic 4制作了一个列表程序,并尝试从rxjs使用BehaviorSubject更新列表。列表可在ngOnInit()期间更新,但列表不会在add()回调时更新 我尝试记录输出,subscribe方法工作(当我单击add时,subscribe被调用),但是爱奥尼亚UI不会更新数据。这是我的密码: 设备管理.service.ts 导出类设备管理服务{ 设备=新行为主体([]); 建造师( 专用存储:存储 ) { } 列表(){ this.storage.get('device-list'

我使用Ionic 4制作了一个列表程序,并尝试从rxjs使用
BehaviorSubject
更新列表。列表可在
ngOnInit()
期间更新,但列表不会在
add()
回调时更新

我尝试记录输出,subscribe方法工作(当我单击add时,subscribe被调用),但是爱奥尼亚UI不会更新数据。这是我的密码:

设备管理.service.ts

导出类设备管理服务{
设备=新行为主体([]);
建造师(
专用存储:存储
) { }
列表(){
this.storage.get('device-list')
.然后(d=>{
如果(d==null)this.devices.next([]);
否则,这个。设备。下一个(d);
})
.catch(e=>{console.log(e)});
}
保存(){
console.log(“保存到存储器”,this.devices.value);
this.storage.set('device-list',this.devices.value);
}
添加(设备){
设x=[].concat(this.devices.value);
x、 反移位(装置);
此.devices.next(x)
这是save();
}
...
第页

。。。
数据=[];
恩戈尼尼特(){
如果(!this.auth.loginStatus.logged.value)this.router.navigateByUrl('/');
this.devices.list();
this.devices.devices.subscribe(设备=>{
这个数据=设备;
console.log(“this.data”,this.data);//我在这里记录订阅。
//它还调用addBtn(),但不更新视图。
});
}
恩贡德斯特罗(){
this.devices.devices.unsubscribe();
}
addBtn(){
add({id:1,nama:“test”});
}
...
page.html

。。。
{{d.nama}}
{{d.id}}
添加设备
...
我哪里做错了,所以我的Ionic 4视图没有更新



已解决:我重新启动了我的
爱奥尼亚服务
,它就可以正常工作。

尝试将此代码放在构造函数方法上:

import {Subscription} from 'rxjs';
subscription: Subscription; // this is a local variable

..........

this.devices.list();
    this.subscription = this.devices.devices.subscribe( devices => {
      this.data = devices;
      console.log("this.data",this.data);   // here i log the subscribe.
      // it also calls on addBtn() , but Ionic View not updating.
    });

应用程序日志显示了什么?

尝试将
数据=[]
数组更改为具有预定义属性
名称
id
的数组。例如,您可以创建:

export class Example {
  nama: string;
  id: string;
}
然后做:

data: Example [];

我不知道在我将该部分添加到我的代码后,它会如何工作,您能详细解释一下吗?您应该创建一个订阅实例,以便能够接收设备订阅的回调