我不能为我的生活弄清楚为什么它一直告诉我" post"未定义,在home.component.ts下面的第20行。你们中的任何一个代码都可以检查我......谢谢
home.component.ts
runaway = 1;
post.ts
import { Component, OnInit } from '@angular/core';
import { Post } from '../_models/index';
import { PostService } from '../_services/index';
@Component({
moduleId: module.id,
templateUrl: 'home.component.html'
})
export class HomeComponent implements OnInit {
posts: Post[] = [];
constructor(private postService: PostService) { }
ngOnInit() {
// get posts from secure api end point
this.postService.getPosts().subscribe(users => {
this.posts = post;
});
}
}
post.service.ts
import { Injectable } from '@angular/core';
import {Author} from "./author";
@Injectable()
export class Post {
id:number;
author:Author;
subscribed:boolean;
created:string;
active:boolean;
text:string;
comments:string[];
constructor(id:number, author:Author, subscribed:boolean, created:string, active:boolean, text:string, comments:string[]) {
this.id = id;
this.author = author;
this.subscribed = subscribed;
this.created = created;
this.active = active;
this.text = text;
this.comments = comments;
}
public static getPosts():Post{
return new Post(0, null, null, "", null, "",null);
}
}
如果您需要,请告诉我。感谢
答案 0 :(得分:1)
因为未声明变量post
。
我猜你试图做这样的事情:
import { Component, OnInit } from '@angular/core';
import { Post } from '../_models/index';
import { PostService } from '../_services/index';
@Component({
moduleId: module.id,
templateUrl: 'home.component.html'
})
export class HomeComponent implements OnInit {
posts: Post[] = [];
constructor(private postService: PostService) { }
ngOnInit() {
// get users from secure api end point
this.postService.getPosts()
.subscribe(post => {
this.posts = post;
});
}
}
问题在于:
.subscribe(user => {
已更改为.subscribe(post => {