在 Angular 5 中更新子组件

Rob*_*lls 3 typescript angular

我正在尝试更新 Angular 5 中的子组件,但无法正常工作。

我的家庭组件通过服务获取数据。

它有一个名为 getTopicToFilter 的函数,该函数由另一个组件更新。这工作正常,并为我提供了TopicId一个@Output EventEmitter.

我的问题是文章没有在子组件中更新

export class HomeComponent implements OnInit {
    loading = true;

    topics: Observable<Topic[]>;
    posts: Observable<Post[]>;

    public constructor(
        private blogService: BlogService
    ) { }


    ngOnInit() {
        this.posts = this.blogService.getPostsByTopic().share()
        // Note that the forkJoin gets other, unrelated data that I have removed from the question
        Observable.forkJoin([
            this.posts
        ]).subscribe(
            response => { },
            error => {
                console.log('An error occurred:', error);
            },
            () => {
                this.loading = false;
            });
    }

    getTopicToFilter(topicId) {
        // I've confirmed I get the right data back from my service based on the topicId
        this.posts = this.blogService.getPostsByTopic(topicId)
    }

}
Run Code Online (Sandbox Code Playgroud)

HomeComponent 的 HTML:

<app-posts [posts]="posts | async"></app-posts>
Run Code Online (Sandbox Code Playgroud)

最后是我的孩子 PostsComponent;

export class PostsComponent{
    @Input() posts: Post[];

    ngOnChanges(changes: SimpleChanges) {
        // only run when property "data" changed
        if (changes['posts']) {
            //  This is always outputting my original insights, not the filtered list
            console.log(this.posts)
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

更新 - 这是我的 BlogService

public getPostsByTopic(topicId = ""): Observable<Post[]> {
   return this.http.get<Post[]>(this.baseUrl + '/getPostsByTopic?TopicId=${topicId}', { headers });
}
Run Code Online (Sandbox Code Playgroud)

Mus*_*.BA 7

export class PostsComponent implements OnChanges {
@Input() posts: Post[];

ngOnChanges(changes: SimpleChanges) {
     for (let propName in changes) {
    // only run when property "data" changed
    if (propName === 'posts') {
         //  this line will update posts values 
         this.posts = changes[propName].currentValue

        console.log(this.posts) 
    }
   }
 }
}
Run Code Online (Sandbox Code Playgroud)