Angular 2 Promise始终返回空ZoneAwarePromise
几天前,我开始学习ASP.NETCore和Angular2。到目前为止,一切都很好,除了今天。 介绍一下我的问题 我有一个非常简单的Web Api控制器Angular 2 Promise始终返回空ZoneAwarePromise,angular,asp.net-core,Angular,Asp.net Core,几天前,我开始学习ASP.NETCore和Angular2。到目前为止,一切都很好,除了今天。 介绍一下我的问题 我有一个非常简单的Web Api控制器 [Route("api/[controller]")] public class HomeController : Controller { private readonly ForumSystemDbContext dbContext; public HomeController(ForumSystemDbCo
[Route("api/[controller]")]
public class HomeController : Controller
{
private readonly ForumSystemDbContext dbContext;
public HomeController(ForumSystemDbContext dbContext)
{
this.dbContext = dbContext;
}
[HttpGet]
public IActionResult Index()
{
var posts = this.dbContext.Posts
.Include(x => x.PostTag)
.ThenInclude(x => x.Tag)
.Select(x => new
{
Title = x.Title,
Content = x.Content,
Tags = x.PostTag.Select
(
y => new
{
name = y.Tag.Name
}
)
});
return this.Json(posts);
}
}
角度服务:
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';
export class Post {
Content: string;
Title: string;
Tags: Tag[];
}
export class Tag {
Name: string;
}
@Injectable()
export class HomeService {
private postsUrl = 'http://localhost:54692/api/home';
constructor(private http: Http) { }
getPosts(): Promise<Post[]> {
return this.http.get(this.postsUrl)
.toPromise()
.then(this.extractData)
.catch(this.handleError);
}
private handleError(error: any) {
// In a real world app, we might use a remote logging infrastructure
// We'd also dig deeper into the error to get a better message
let errMsg = (error.message) ? error.message :
error.status ? `${error.status} - ${error.statusText}` : 'Server error';
console.error(errMsg); // log to console instead
return Promise.reject(errMsg);
}
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
}
问题是posts变量始终是空数组。当我登录时
this.homeService.getPosts()
它的回报
ZoneAwarePromise {__zone_symbol__state: null, __zone_symbol__value: Array[0]}
我已经阅读了angular 2的官方文档,但不幸的是,它在我的案例中不起作用
所以,我想知道我的错误在哪里
这是某种配置问题还是其他问题?请尝试此服务方法
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class HomeService {
private postsUrl = 'http://localhost:54692/api/home';
constructor(private http:Http) { }
getPosts(){
return this.http.get(this.postsUrl).map(
res => res.json()
);
}
}
并从下面的组件中使用它
getPosts(){
this.homeService.getPosts().subscribe(
data => {
this.posts = data;
},
error => {
},
() => {}
);
}
这里,this.homeService
是您服务的一个实例,this.posts
只是一个局部变量,它将由您从Web API返回的帖子填充
主页有帮助。尝试更改:
private extractData(res: Response) {
let body = res.json();
return body.data || {};
}
到
您的响应中没有数据
属性
[{"Title":"title1","Content":"","Tags":[]},{"Title":"title2","Content":"","Tags":[]}]
快速提问。您在extractData中使用body.data,但我在API中的任何位置都看不到.data。我错过什么了吗?另外,如何记录getPosts()?必须
导入'rxjs/Rx'代码>@PankajParkar我做了,但结果是一样的。@Bojankogj我只是按照Angular 2入门教程做的。我将getPosts()记录在ngOnInit()的home.component.ts中。@BojanKogoj他正在将http响应转换为JSON响应,然后使用点符号访问JSON对象成员。这种方法也可以,但我更喜欢使用承诺。无论如何,谢谢你。
private extractData(res: Response) {
return res.json();
}
[{"Title":"title1","Content":"","Tags":[]},{"Title":"title2","Content":"","Tags":[]}]