类型'Observable <response>'Nativescript上不存在'map'

时间:2018-06-13 12:50:08

标签: angular typescript rxjs observable nativescript

我需要在Nativescript中通过Web服务获取所有警报。但.map不起作用。我试过这个功能。在Angular 5中运行良好,但在Nativescript中显示此错误:

[ts]'Observable'类型中不存在属性'map'。在此.map((响应:响应)=&gt; {

我导入了这个:import 'rxjs/add/operator/map'; import { Http, Headers, Response } from '@angular/http';  但没有任何反应

我的“rxjs”:“~6.1.0”,

 public AlarmsGetAll(): Observable<Alarm[]> {
    let headers = new Headers();
    headers.append('x-access-token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOnsiVXNlcklEIjoiMzEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAifSwiaWF0IjoxNTI4ODgzNDU1LCJleHAiOjE1Mjk3NDc0NTV9.qei48rzt1KF9AKIEz8Aq-A8pWkESc1G3jBUfdU27oYE');
    return this.http.get(Api.getUrl(Api.URLS.AlarmsGetAll), {
      headers: headers
    })
      .map((response: Response) => {
        let res = response.json();
         console.log(res)
        if (res.StatusCode === 1) {
        } else {
          return res.StatusDescription.map(alarm => {
            //console.log(alarm)
            return new Alarm(alarm);
          });
        }
      });
  }

你能问我一个想法,问题是什么?

3 个答案:

答案 0 :(得分:2)

如果使用rxjs @ 6,则应更新运算符。

Rxjs 5

import 'rxjs/add/operator/map'

myObservable
  .map(yourdata=> yourdata + 15)
  .subscribe(...);

Rxjs 6

import { map } from 'rxjs/operators';

myObservable
  .pipe(map(yourdata=> yourdata + 15))
  .subscribe(...);

您案例中的解决方案:

因此您需要更新导入并使用 Rxjs6 的新方式

import { map } from 'rxjs/operators';

 public AlarmsGetAll(): Observable<Alarm[]> {
    let headers = new Headers();
    headers.append('x-access-token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOnsiVXNlcklEIjoiMzEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAifSwiaWF0IjoxNTI4ODgzNDU1LCJleHAiOjE1Mjk3NDc0NTV9.qei48rzt1KF9AKIEz8Aq-A8pWkESc1G3jBUfdU27oYE');
    return this.http.get(Api.getUrl(Api.URLS.AlarmsGetAll), {
      headers: headers
    })
      .pipe(map((response: Response) => {
        let res = response.json();
         console.log(res)
        if (res.StatusCode === 1) {
        } else {
          return res.StatusDescription.map(alarm => {
            //console.log(alarm)
            return new Alarm(alarm);
          });
        }
      }));
  }

If you need more information

答案 1 :(得分:1)

以下是使用最新版本的rxjs v6和HttpClientModule修改的代码(自v5起不再使用HttpModule):

import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

public AlarmsGetAll(): Observable<Alarm[]> {
let headers = new HttpHeaders();
headers.append('x-access-token', 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyaWQiOnsiVXNlcklEIjoiMzEwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAifSwiaWF0IjoxNTI4ODgzNDU1LCJleHAiOjE1Mjk3NDc0NTV9.qei48rzt1KF9AKIEz8Aq-A8pWkESc1G3jBUfdU27oYE');
return this.http.get(Api.getUrl(Api.URLS.AlarmsGetAll), {
  headers: headers,
  observe: 'response'
}).pipe(
  map((response: HttpResponse<any>) => {
    let res = response.json();
     console.log(res)
    if (res.StatusCode === 1) {
    } else {
      return res.StatusDescription.map(alarm => {
        //console.log(alarm)
        return new Alarm(alarm);
      });
    }
  }));
}

如前所述,您可以查看迁移准则以将rxjs v5更新为v6,以下是HttpClientModule的文档: Angular http documentation

答案 2 :(得分:0)

最新的NativeScript使用Angular 6和Rxjs 6.1.0及更高版本。检查从rxjs @ 5移动到rxjs @ 6

的迁移准则