Angular 角度Http承诺返回未定义

Angular 角度Http承诺返回未定义,angular,http,typescript,undefined,Angular,Http,Typescript,Undefined,只是尝试从http结果设置我的组件属性,但没有成功。谢谢你的帮助!(使用静态模拟对象) 类对象 export class Gallery { name: string; } 服务 import { Injectable } from '@angular/core'; import { Http } from '@angular/http'; import 'rxjs/Rx'; import { Observable } from 'rxjs/Observable'; import '

只是尝试从http结果设置我的组件属性,但没有成功。谢谢你的帮助!(使用静态模拟对象)

类对象

export class Gallery {
    name: string;
}

服务

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
import { Gallery } from './GalleryModel';


@Injectable()
export class GalleryLoadService {

    private galleryUrl = 'api/Gallery/Get'; 

    constructor(private http: Http) {
    }


    getGalleries(): Promise<Gallery[]> {
        return this.http.get(this.galleryUrl)
            .toPromise()
            .then(response => response.json().data as Gallery[])
            .catch(this.handleError);
    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); 
        return Promise.reject(error.message || error);
    }
}

如果这是从API获得的结果,那么您不应该在
getGalleries
方法中使用
response.json().data
,因为没有
data
属性。删除
数据

.then(response => response.json() as Gallery[])
见:

不要对服务器API进行任何假设。并非所有服务器都返回 具有
数据
属性的对象


如果这是从API获得的结果,那么您不应该在
getGalleries
方法中使用
response.json().data
,因为没有
data
属性。删除
数据

.then(response => response.json() as Gallery[])
见:

不要对服务器API进行任何假设。并非所有服务器都返回 具有
数据
属性的对象


你让我开心!非常感谢。你让我开心!非常感谢。
.then(response => response.json() as Gallery[])