可观察的<{}>不可分配给类型 Observable<SomeType[]> [英] Observable&lt;{}&gt; not assignable to type Observable&lt;SomeType[]&gt;

查看:26
本文介绍了可观察的<{}>不可分配给类型 Observable<SomeType[]>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习 Angular2 和 Typescript.我正在学习 angular.io 上的 Heroes 教程,但将其应用到我从 ASP.Net 转换的项目中.我遇到了一个问题,我认为这是由于我缺乏理解,但据我所知,它与教程的相关部分正在做的事情相匹配.

I'm learning Angular2 and Typescript. I'm working through the Heroes tutorial on angular.io, but applying it to a project I'm converting from ASP.Net. I've run into a problem which I think is due to my lack of understanding, though as far as I can see it matches what the relevant part of the tutorial is doing.

import { Injectable } from '@angular/core';
import {RiskListSummary} from '../Models/RiskListSummary';
import { Observable } from 'rxjs/Rx';
import { Http, Response } from '@angular/http';

@Injectable()
export class RiskAssessmentListService {

    constructor(private http : Http) {}

    private serviceUrl = "http://myserviceurl/";

    getRisks(): Observable<RiskListSummary[]> {
        return this.http.get(this.serviceUrl)
            .map(this.extractData())
            .catch(this.handleError());
    }

    private extractData(res: Response) {
       if (res.status < 200 || res.status >= 300) {
             throw new Error('Bad response status: ' + res.status);
           }
       let body = res.json();
       return body.data || { };
    }

    private handleError (error: any) {
        let errMsg = error.message || 'Server error';
        console.error(errMsg); // log to console instead
        return Observable.throw(errMsg);
    }
}

我在return this.http.get(this.serviceUrl)"行上收到以下错误:

I'm getting the following error on the line "return this.http.get(this.serviceUrl)":

错误:(20, 16) TS2322:类型Observable<{}>"不可分配给类型'可观察'.类型{}"不可分配给类型'风险清单摘要[]'.类型{}"中缺少属性长度".

Error:(20, 16) TS2322: Type 'Observable<{}>' is not assignable to type 'Observable'. Type '{}' is not assignable to type 'RiskListSummary[]'. Property 'length' is missing in type '{}'.

如果它有所不同,我正在使用 webstorm(最新版本),但我认为这个错误直接来自打字稿编译器.我在想也许我需要一个用于 rxjs 的打字文件,但本教程没有使用,而且我通过打字搜索"找到的文件都没有区别

If it makes a difference, I'm using webstorm (latest version), but I think this error is coming straight from the typescript compiler. I was thinking maybe I need a typings file for rxjs, but the tutorial doesn't use one, and none of the ones that I found with a "typings search" made a difference

以下是我在 package.json 中的依赖项:

The following are my dependencies from package.json:

  "dependencies": {
    "@angular/common":  "2.0.0-rc.1",
    "@angular/compiler":  "2.0.0-rc.1",
    "@angular/core":  "2.0.0-rc.1",
    "@angular/http":  "2.0.0-rc.1",
    "@angular/platform-browser":  "2.0.0-rc.1",
    "@angular/platform-browser-dynamic":  "2.0.0-rc.1",
    "@angular/router":  "2.0.0-rc.1",
    "@angular/router-deprecated":  "2.0.0-rc.1",
    "@angular/upgrade":  "2.0.0-rc.1",

    "systemjs": "0.19.27",
    "es6-shim": "^0.35.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.6",
    "zone.js": "^0.6.12"

推荐答案

我认为您的问题位于此处:

I think that your problem is located here:

getRisks(): Observable<RiskListSummary[]> {
  return this.http.get(this.serviceUrl)
     .map(this.extractData()) <== passing result of function
     .catch(this.handleError()); <== passing result of function
}

你可以只使用传递 function 引用:

You could use just passing function reference:

getRisks(): Observable<RiskListSummary[]> {
  return this.http.get(this.serviceUrl)
     .map(this.extractData)
     .catch(this.handleError);
}

但是这样你会丢失this.

或者你可以使用 bind 方法保留this:

Or you could use bind method to retain this:

getRisks(): Observable<RiskListSummary[]> {
  return this.http.get(this.serviceUrl)
    .map(this.extractData.bind(this))
    .catch(this.handleError.bind(this));
}

但是你会失去类型检查.

我会利用 arrow 函数来能够使用词法this:

I would leverage arrow functions to be able to use lexical this:

getRisks(): Observable<RiskListSummary[]> {
  return this.http.get(this.serviceUrl)
    .map(res => this.extractData(res))
    .catch(err => this.handleError(err));
}

没有它,this 变量将指向进行调用的函数,而不是包含 getRisks() 的实例.

Without it, the this variable will point to the function where the call is made, instead of the instance that contains getRisks().

这篇关于可观察的<{}>不可分配给类型 Observable<SomeType[]>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆