MAR*_*att 1 javascript typescript visual-studio-2017 angular
我是angularJs2的新手.我创建了以下服务:
import { Injectable, OnInit } from '@angular/core';
import { customType } from '../models/currentJobs';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
@Injectable()
export class JobService implements OnInit {
constructor(private http: Http) { }
ngOnInit(): void {
this.getCurrentJobs();
}
private headers: Headers = new Headers({ 'Content-Type': 'application/json' });
private ordersUrl: string = 'http://localhost:35032/api/order/';
public orders: customType[];
getCurrentJobs(): Promise<customType[]> {
var jobs = this.http.get(this.ordersUrl)
.toPromise()
.then(response => {
this.orders = response.json() as customType[];
})
.catch(this.handleError);
return jobs;//this line throws error
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是Vs2017的Typescript编译配置
当我使用visual studio 2017编译代码时,我得到以下错误
**TS2322 Build:Type 'Promise<void>' is not assignable to type 'Promise<customType[]>'.*
*
帮我解决这个错误.
你是不是给您回里的任何东西then
,这使得jobs
具有类型Promise<void>
.返回内部数组then
:
getCurrentJobs(): Promise<customType[]> {
var jobs = this.http.get(this.ordersUrl)
.toPromise()
.then(response => {
this.orders = response.json() as customType[];
return this.orders;
})
.catch(this.handleError);
return jobs;
}
Run Code Online (Sandbox Code Playgroud)
查看promises的链接行为:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining
归档时间: |
|
查看次数: |
11810 次 |
最近记录: |