【发布时间】:2019-06-21 08:48:49
【问题描述】:
我正在尝试获取单个帖子的详细信息,但我不断收到此错误;错误类型错误:无法读取 Object.eval [as updateRenderer] 处未定义的属性“标题”。 虽然数据在 console.log 中被正确获取,但它没有显示在页面本身上。当我执行 {{post?.title}} 时,错误消失了,但结果仍然没有出现在页面上,但在 console.log 中正确显示
离子框架:v4 操作系统平台:Windows 10
post.service
getAllPosts(): Observable<any> {
return this.http.get(`${this.url}`).pipe(
map(results => {
console.log('Raw: ', results);
return results['posts'];
})
);
}
getPost(postId: string) {
return this.http.get(`${this.url}/${postId}`);
}
post.page.ts
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { PostService } from '../post.service';
@Component({
selector: 'app-post',
templateUrl: './post.page.html',
styleUrls: ['./post.page.scss'],
})
export class PostPage implements OnInit {
loadedPost: any;
constructor(private activatedRoute: ActivatedRoute, private postService: PostService) { }
ngOnInit() {
const postId = this.activatedRoute.snapshot.paramMap.get('postId');
this.postService.getPost(postId).subscribe(result => {
console.log('details: ', result);
this.loadedPost = result;
});
}
}
post.page.html
<ion-card-header>
<ion-card-title text-center>{{loadedPost.title}}</ion-card-title>
</ion-card-header>
console.log 输出
post:
author_id: 0
body: "<p>This is the body of the lorem ipsum post</p>"
category_id: null
created_at: "2019-06-13 17:08:45"
excerpt: "This is the excerpt for the Lorem Ipsum Post"
featured: 0
id: 1
image: "posts/post1.jpg"
meta_description: "This is the meta description"
meta_keywords: "keyword1, keyword2, keyword3"
seo_title: null
slug: "lorem-ipsum-post"
status: "PUBLISHED"
title: "Lorem Ipsum Post"
console.log 截图: Screenshot of console.log response
【问题讨论】:
-
您能否尝试在构造函数中注入区域,如下所示:
constructor(private ngZone: NgZone, ...) {},然后执行this.ngZone.run(() => { this.loadedPost = result; })?如果可行,我将添加一个提供更多信息的答案。 -
那也没用
-
我想我已经找到了问题,请在下面查看我的答案