简体   繁体   English

Angular2:通过路由器在同级组件之间传递数据吗?

[英]Angular2: Passing data between sibling components through routers?

I'm new to Angular2. 我是Angular2的新手。 I've been learning what I have mainly though the official docs, a slightly outdated udemy course by Mosh, and a book called ng-book2. 我一直在通过官方文档,Mosh稍微过时的udemy课程以及一本名为ng-book2的书来学习我所学的东西。

What I have is a form that always exists on the (top of the) page. 我所拥有的是总是在页面顶部的表格。 Underneath it is a listing from the database. 它的下面是数据库列表。 Clicking an item from the listing replaces the entire listing with details of that item. 单击列表中的一个项目,将整个列表替换为该项目的详细信息。 Clicking the back button takes you back to the listing. 单击后退按钮可将您带回到清单。 The form remains on top. 表格仍然在最上面。 It's a basic CRUD application. 这是一个基本的CRUD应用程序。 Submitting the form saves a new item to the database. 提交表单会将新项目保存到数据库。

The problem is when I submit the form, the listing does not automatically get the new item: instead I have to refresh the page. 问题是当我提交表单时,列表不会自动获取新项目:相反,我必须刷新页面。 Other operations (upvote, downvote, delete) are working fine. 其他操作(upvote,downvote,delete)工作正常。

app.component.html: app.component.html:

<app-article-form [listid]="listid" [formid]="formid"></app-article-form>
<router-outlet></router-outlet>

The router outlet displays either the item listing or an item detail. 路由器出口显示物品清单或物品详细信息。

Program architecture : I have a separate component for the form (ArticleFormComponent), a separate component for the listing (ArticlesComponent) and a separate component for detail (ArticleDetailComponent). 程序体系结构 :我有一个用于表单的单独组件(ArticleFormComponent),一个用于列表的单独组件(ArticlesComponent)和一个用于详细信息的单独组件(ArticleDetailComponent)。 Routing is between ArticlesComponent and ArticleDetailComponent. 路由在ArticlesComponent和ArticleDetailComponent之间。

I basically want ArticleFormComponent to notify it's sibling ArticlesComponent that a new Article has been submitted, and I want ArticlesComponent to receive that Article and push() it in the Articles[] array. 我基本上是希望ArticleFormComponent通知其同级ArticlesComponent已提交了新的Article,并且我希望ArticlesComponent接收该Article并将其push()在Articles []数组中。

I googled a bit and tried to implement an emitter service to broadcast the event but the problem is that I'm using a router-outlet and do not know how to set input properties in that. 我用google搜索了一下,试图实现一个发射器服务来广播事件,但是问题是我使用的是路由器插座,却不知道该如何设置输入属性。 Could someone guide me in the right direction? 有人可以指引我正确的方向吗? Thanks. 谢谢。

You could for example implement a PubSub pattern using simply RxJS's ReplySubject class. 例如,您可以仅使用RxJS的ReplySubject类来实现PubSub模式。 Here is how: 方法如下:

import { Injectable } from '@angular/core';
import { ReplaySubject } from 'rxjs';

@Injectable()
export class ArticlesPubSubService extends ReplaySubject<IArticle> {

  constructor() {
    super();
  }

}

Then use this ArticlesPubSubService in both components: 然后在两个组件中都使用此ArticlesPubSubService

1) in articles-form.component.ts you would emit the new created article: 1)在articles-form.component.ts您将发出新创建的文章:

import { Component, OnInit } from '@angular/core';
import { ArticlesPubSubService } from '../articles-pub-sub.service';

@Component({
  selector: 'so-articles-form',
  templateUrl: './articles-form.component.html',
  styleUrls: ['./articles-form.component.css']
})
export class ArticlesFormComponent implements OnInit {

  constructor(private aps: ArticlesPubSubService) { }

  ngOnInit() {
  }

  submit(article) {
    // ... 
    this.aps.next(article);
  }

}

2) In articles.component.ts you would get this new article and push it to your local articles list: 2)在articles.component.ts您将获得此新文章并将其推送到本地文章列表:

import { Component, OnInit } from '@angular/core';
import { ArticlesPubSubService } from '../articles-pub-sub.service';

@Component({
  selector: 'so-articles',
  templateUrl: './articles.component.html',
  styleUrls: ['./articles.component.css']
})
export class ArticlesComponent implements OnInit {

  articles: IArticles[];

  constructor(private aps: ArticlesPubSubService) { }

  ngOnInit() {
    this.aps.subscribe( article => this.articles.push(article) );
  }
  ngOnDestroy() {
    this.aps.unsubscribe();
  }
}

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

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