繁体   English   中英

Angular 4 RxJs Observable Subjects数组未使用新对象更新

[英]Angular 4 RxJs Observable Subjects array not updating with new object

因此,最近我了解了主题,并试图在个人项目中使用它们。 我有一个从json文件中获取数据并将其转换为“ Article”类型的服务。 文章是一个自定义类,用于保存有关博客文章的信息。

我这里的最终目标是获取此文章数组,然后当我按+按钮时,它将新的空白Article添加到当前列表中,并且该视图应通过显示具有一些默认值的空白文章来表示它。 这不会保留(另存为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.articles属性似乎已使用新的空白文章进行了更新,但是视图并没有改变,我不能肯定地说,但是似乎该文章很快就丢失了无论如何尽快添加。 可观察到的重复http get和擦洗文章列表是否再次干净?

您实际上尚未在显示组件中订阅您感兴趣的主题。 您只订阅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
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM