Angular 4 RXJ可观察到的主体数组不使用新对象更新



所以最近我已经了解了主题,并且我正在尝试在个人项目中使用它们。我有一项服务,可以从JSON文件中获取数据并将其投入到"文章"类型中。文章是一个自定义类,在博客文章中拥有信息。

我的最终目标是获取此文章数组,然后当我按A 按钮时,它将新的空白文章添加到当前列表中,并且视图应通过显示空白的文章中的一些默认文章来表示值。这不会持续(保存到JSON)新的空白文章,而只需将其添加到当前列表中,以便查看更新并显示它。储蓄将稍后再来。

我一生都无法实现。所有文章都正确地显示在我的"文章列表"页面上,但手动将空白推向它似乎根本没有做任何事情。

这是我的服务文件

@Injectable()
export class ArticleService {
  headers: Headers;
  options: RequestOptions;
  articles: ReplaySubject<Article[]>;
  private url = 'data/articles.json';
  constructor(private http: Http) { 
    this.headers = new Headers({ 'Content-Type': 'application/json' });
    this.options = new RequestOptions({ headers: this.headers });
    this.articles = new ReplaySubject<Article[]>();
  }
  /**
   * fetch a list of articles
   */
  getArticles(): Observable<Article[]> {
    // no articles fetched yet, go get!
    return this.http.get(this.url, this.options)
                    .map(response => <Article[]>response.json())
                    .do(data => this.articles.next(data))
                    .catch(this.handleError);
  }
  /**
   * add a new article to the list
   * @param article 
   */
  addArticle(article: any): void {
    this.getArticles().take(1).subscribe(current => {
      //
      //
      // THIS WORKS. this.articles is UPDATED successfully, but the view doesn't update
      // IT ALSO LOOKS LIKE this.articles may be being reset back to the result of the get()
      // and losing the new blank article.
      //
      //
      current.push(article);
      this.articles.next(current);
    });
  }
  ...
}

我有一个列表组件,以这样更新列表:

export class ArticleListComponent implements OnInit {
    articles: Article[];
    public constructor(private articleService: ArticleService) { }
    ngOnInit(): void {
      this.getArticles();
    }
    getArticles(): void {
      this.articleService.getArticles().subscribe(articles => this.articles = articles);
    }
}

和另一个创建新空白文章的组件:

export class CreatorComponent {
    articles: Article[];
    public constructor(private articleService: ArticleService) { }
    /**
     * add a new empty article
     */
    add(): void {
        let article = {};
        article['id'] = 3;
        article['author'] = "Joe Bloggs";
        article['category'] = "Everyday";
        article['date'] = "November 22, 2017"
        article['image'] = "/assets/images/post-thumb-m-1.jpg";
        article['slug'] = "added-test";
        article['title'] = "New Via Add";
        article['content'] = "Minimal content right now, not a lot to do."
        this.articleService.addArticle(article);
    }
}

我可以调试,并且该服务上的this.this.Articles属性似乎已随附新的空白文章,但视图没有改变,我无法确定,但是看来这篇文章只是无论如何,它都会立即丢失。可观察的重复HTTP是否再次清除了文章清单?

您实际上没有订阅您在显示组件中感兴趣的主题。您仅订阅填充您感兴趣的主题然后终止的HTTP调用。尝试更多地这样:

private articleSub: Subscription;
ngOnInit(): void {
  this.articleSub = this.articleService.articles.subscribe(articles => this.articles = articles);
  this.articleService.getArticles().subscribe();
}
ngOnDestroy() { //make sure component implements OnDestroy
    this.articleSub.unsubscribe(); // always unsubscribe from persistent observables to avoid memory leaks
}

最新更新