2016-04-03 36 views
1

在角1打字稿我能够创造的服务,我知道的返回类型,我会得到相同的数字数组或一个Person对象:角2 HTTP强类型的返回类型

public addAges(ages: number[]) : ng.IPromise<number[]> { 
    return this.$http.post('Proxy/AddAges',ages) 
        .then((response: ng.IHttpPromiseCallbackArg<number[]>) : number[] 
         => { return response.data; }); 
} 

在角2我我创建了以下服务,目前为止工作正常,但我不知道在哪里可以设置类似于Angular 1中的完全类型的回归类型,以及IPromise和我的情况下的人员数组。

import {Injectable} from 'angular2/core'; 
import {Http, Response} from 'angular2/http'; 
import {Person} from './../customDataClasses/Person' 
import 'rxjs/add/operator/map'; 

@Injectable() 
export class CourseService { 
    constructor(private _http: Http) { } 
    getPersons(name:string) { 
     return this._http.get(`Home/GetPersons?name=${name}`) 
          .map((res : Response) => res.json()); 
    } 
} 

回答

2

我想这是你想要什么:

import { Observable } from 'rxjs/observable'; 
import 'rxjs/add/operator/map'; 

... 

    getPersons(name:string):Observable<number[]> { 
     return this._http.get(`Home/GetPersons?name=${name}`) 
          .map((res : Response) => res.json()); 
    } 
+0

是 - 但“可观察的”不从我的打字稿 – squadwuschel

+0

发现您可以从rxjs导入观测量/接收像进口{可观察}从' rxjs /接收'; – Abhinandan

+0

你是否知道如果没有更有选择性地导入,thos会对构建输出大小产生负面影响?我自己不使用TS,也没有深入的知识。 –