将代码从Angular 5迁移到Angular 7时出现问题

时间:2018-11-30 17:19:31

标签: angular rxjs5 angular7 rxjs6

我正在尝试将一些代码从Angular 5迁移到Angular7。我尝试了官方更新指南,但并非所有内容都已迁移。因此,我手动进行了尝试,但无法正常工作。

这是Angular 5代码

getNodes(): Observable < Node[] > {
   return Observable.create(obs => {
     ros.getNodes(data => {
      obs.next(data);
      obs.complete();
     });
   }).flatMap(data => {
     return Observable.forkJoin(data.map(name => Observable.create(obs => {
       const detailClient = new ROSLIB.Service({
         ros: ros,
         name: '/rosapi/node_details',
         serviceType: 'rosapi/NodeDetails'
       });
       const request = new ROSLIB.ServiceRequest({ node: name });
       detailClient.callService(request, data2 => {
         obs.next({ name, subscribing: data2.subscribing });
         obs.complete();
       });
     })));
   });
 }
 loadData(): void {
   Observable.forkJoin(this.getNodes()).subscribe(([nodes]) => {
     //this.data.nodes = nodes;
     console.log(nodes);
   });
 }

console.log的输出如下,新的Angular 7代码应该看起来相同

[
  {
    "name": "/mission_controller"
  },
  {
    "name": "/mavros"
  },
  {
    "name": "/pose_analyser"
  }
]

这是我尝试过的方法,但是它给我错误“类型'{}'上不存在属性'管道'”

getNodes(): Observable<Node[]> {
  return Observable.create(obs => {
    ros.getNodes(data => {
      obs.next(data);
      obs.complete();
    });
  }).pipe(mergeMap( data => {
    return forkJoin(data.pipe(map(name => Observable.create(obs => {
      const detailClient = new ROSLIB.Service({
        ros: ros,
        name: '/rosapi/node_details',
        serviceType: 'rosapi/NodeDetails'
      });
      const request = new ROSLIB.ServiceRequest({ node: name });
      detailClient.callService(request, data2 => {
        obs.next({ name });
        obs.complete();
      });
    }))));
  }));
}
loadNodes(): void {
  forkJoin(this.getNodes()).subscribe(([nodes]) => {
    console.log(nodes);      
  });
}

我希望有人能帮助我

最诚挚的问候

newsub7

0 个答案:

没有答案