简体   繁体   English

Angular 9 中的组件之间共享数据

[英]Sharing data between components in Angular 9

I have a application with a custom library ( tools ) containing a number of components to be shared with other projects in the application (let's call one of them data-portal ).我有一个带有自定义库( tools )的应用程序,其中包含许多要与应用程序中的其他项目共享的组件(我们称其中一个为data-portal )。

For some context, in the data-portal app.component.html I have:在某些情况下,在data-portal app.component.html我有:

<div>
  ...
  <lib-primeng-menu-nav></lib-primeng-menu-nav>
  <router-outlet></router-outlet>
  ...
</div>

where the lib-primeng-menu-nav , from the tools library, contains the navigation for the project and router-outlet can show a few different views, one of which shows a number of charts with time series data (also from the tools library):其中lib-primeng-menu-nav ,来自tools库,包含项目的导航, router-outlet可以显示几个不同的视图,其中一个显示许多带有时间序列数据的图表(也来自tools库): 时间序列图表示例

When a user clicks on the star icon at the bottom left of the charts, it should toggle that series from being included in an 'Analyzer' view, where multiple series can be drawn on a single chart for comparison.当用户单击图表左下角的星形图标时,它应该将该系列从包含在“分析器”视图中切换,其中可以在单个图表上绘制多个系列以进行比较。 I have an Analyzer Service in tools meant to keep track of which series should be displayed in the Analyzer.我在tools中有一个分析器服务,用于跟踪应在分析器中显示哪些系列。

Back to the navigation bar, the link to the Analyzer should also display a counter of how many series have been selected:返回导航栏,Analyzer 的链接还应显示已选择的系列数量的计数器:

<nav id="sidebar-nav" class="navbar navbar-fixed-top navbar-light">
  ...
  <a routerLink="/analyzer">&nbsp; Analyzer ({{analyzerSeries}})</a>
  ...
</nav>

And here is my issue: I can't get the counter to change.这是我的问题:我无法让柜台改变。 On initial load, the link shows as Analyzer (0) as it should, but whenever I click on one of the series to be added, the counter remains at 0. If I navigate to the Analyzer view, the display correctly shows the right series in the chart, but the navigation bar still shows (0) in the counter.在初始加载时,链接应显示为Analyzer (0) ,但每当我单击要添加的系列之一时,计数器保持为 0。如果我导航到 Analyzer 视图,则显示正确显示正确的系列在图表中,但导航栏仍然在计数器中显示(0) All other behavior related to the analyzer view & service seems to work fine.与分析器视图和服务相关的所有其他行为似乎都可以正常工作。

Here is what I have tried so far:这是我到目前为止所尝试的:

Method 1方法一

analyzer.service.ts :分析器.service.ts

@Injectable({
  providedIn: 'root'
})
export class AnalyzerService {
  // Keep track of series in the analyzer
  public analyzerSeries = [];
  public analyzerData = {
    analyzerTableDates: [],
    analyzerSeries: [],
  };

primeng-menu-nav.component.ts primeng-menu-nav.component.ts

  constructor(
    private _analyzerService: AnalyzerService,
    private route: ActivatedRoute,
    private _router: Router
  ) { }
ngOnInit() {
    ...
    this.analyzerSeries = this._analyzerService.analyzerSeries.length;
    ...
  }

primeng-menu-nav.component.html primeng-menu-nav.component.html

<nav id="sidebar-nav" class="navbar navbar-fixed-top navbar-light">
  ...
  <a routerLink="/analyzer">&nbsp; Analyzer ({{analyzerSeries}})</a>
  ...
</nav>

Method 2方法二

analyzer.service.ts分析器.service.ts

  public analyzerSeries = [];
  private analyzerSeriesTest = new BehaviorSubject(null);
  analyzerSeries$ = this.analyzerSeriesTest.asObservable();
  ...
  updateAnalyzer(seriesId) {
    ...
    this.analyzerSeriesTest.next(this.analyzerSeries.length);
    ...
  }

primeng-menu-nav.component.ts primeng-menu-nav.component.ts

constructor(
    public _analyzerService: AnalyzerService,
    private route: ActivatedRoute,
    private _router: Router
  ) {
    this.analyzerSeriesCount = this._analyzerService.analyzerSeries$.subscribe((data: any) => {
      this.analyzerSeries = data;
    });
  }

Neither of these have worked.这些都没有奏效。 I've also tried using an EventEmitter instead of a Behavior Subject in the service for the component to subscribe to, and I'm hitting a wall.我还尝试在服务中使用 EventEmitter 而不是 Behavior Subject 来让组件订阅,但我碰壁了。 My hunch is that the service isn't being provided correctly, but I'm not completely sure.我的预感是服务没有正确提供,但我不完全确定。 As I understand,我认为,

@Injectable({
  providedIn: 'root'
})

provides the service as singleton to be available throughout the app.提供 singleton 服务,可在整个应用程序中使用。 I do not have the service listed as a provider in any of my modules.我的任何模块中都没有将服务列为提供者。

Finally figured it out, so figured I'd post an answer in case.终于想通了,所以想我会发布一个答案以防万一。 I had an extra 'ShareModule' in the tools library that I was importing to the data-portal app.module.ts to import the components.我在导入data-portal app.module.ts 的tools库中有一个额外的“ShareModule”以导入组件。 Just realized it was unnecessary since I could just import the tools.module.ts instead, and removing ShareModule from the imports fixed my issue with counter.刚刚意识到这是不必要的,因为我可以只导入tools.module.ts ,并从导入中删除 ShareModule 解决了我的计数器问题。 Guess that caused in issue with providing the Analyzer service.猜猜这是导致提供分析器服务出现问题的原因。

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

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