import {Post} from './post.model'; import {Injectable} from '@angular/core'; import {Subject} from 'rxjs'; import {HttpClient} from '@angular/common/http'; @Injectable({providedIn: 'root'}) export class PostService{ private posts: Post[] = []; private postUpdated = new Subject(); constructor(private http: HttpClient){ } getPosts(){ this.http.get<{message: string, posts: Post[]}>('http://localhost:3000/api/posts') .subscribe((PostData) => { this.posts = PostData.posts; this.postUpdated.next([...this.posts]); }); } addPost(title: string, content: string){ const post: Post={id: '', title: title, content: content}; this.http.post<{message: string}>('http://localhost:3000/api/posts', post). subscribe((responseData) => { console.log(responseData.message); this.posts.push(post); this.postUpdated.next([...this.posts]); }); } getPostUpdateListener() { return this.postUpdated; } }