Angular 两个依赖组件,第二个组件的更改必须更改第一个组件

发布于 2025-01-12 19:47:25 字数 1093 浏览 3 评论 0原文

同一页上有一个标签和一个多行表格。标签需要与表格同步。

组件 1

分析总数 = 2

组件 2(表格)

筛选依据:[无]

  1. 分析 1 的样本
  2. 分析 1 的
  3. 样本 分析 2 的样本

您会看到组件 1 取决于2. 问题是组件 2 中的表有一个过滤器输入字段。当您过滤并按 Enter 时,它会执行 API 调用。返回的列表可能会有所不同,因为我们应用程序的另一部分添加了新的分析。


组件 1

分析总数 = 2(问题:过滤器调用后的值相同。其他页面的某人添加了分析。)

组件 2 (表格)

过滤依据:[1,2,3]

  1. 分析 1 的样本
  2. 分析 1 的样本
  3. 分析 2 的
  4. 样本 分析 3 的
  5. 样本 分析 3 的样本

添加了来自另一个屏幕的分析 2,3,4。正确的总计应该是 4,而不是 2。但是过滤器 API 只更新了表。 (分析 4 中的样本与过滤器不匹配)。


(!!) 您无法从表中计算或推断总计。因此您无法使用输出事件进行更新。因为过滤器不会返回所有值。

我对解决方案的想法

我曾想过对标签和表格使用一个 GET 调用。一次调用即可立即检索过滤后的表和总分析,从而解决了同步问题。但标签和表格需要可重复使用,是两个独立的组件。

因此,我需要两个组件和一个调用(以解决同步问题)。 因此,我为该 GET 调用创建了一个组件,然后使用 Input() 将标签数据发送到组件 1,将表数据发送到组件 2。

问题 我进行了一次激烈的头脑风暴,考虑使用 SignalR 和任何其他解决方案。这看起来是一个很常见的场景,所以我想问一下。您会使用什么解决方案?

编辑示例以强调无法从表数据计算标签数据。

A label and a table with multiple rows on the same page. Label needs to be synced with table.

Component 1

Total of analyses = 2

Component 2 (a table)

filterBy: [none]

  1. Sample from analysis 1
  2. Sample from analysis 1
  3. Sample from analysis 2

You see component 1 depends on 2. Problem is the table in Component 2 has a filter input field. When you filter and press Enter it does an API call. The list returned might be different because from another part of our app a new analyses have been added.


Component 1

Total of analyses = 2 (Problem: same value after filter call. Someone from another page added analyses.)

Component 2 (a table)

filterBy: [1,2,3]

  1. Sample from analysis 1
  2. Sample from analysis 1
  3. Sample from analysis 2
  4. Sample from analysis 3
  5. Sample from analysis 3

From another screen analyses 2,3,4 were added. Correct Total should be 4, not 2. But filter API only updated the table. (Sample from analysis 4 did not match filter).


(!!) You cannot compute or deduce the Total from the table. So you cannot update using Output events. Because filter does not return all values.

My thoughts on solutions

I have thought to use one GET call for label and for table. One call retrieves the filtered table and the total analyses at once, solving the sync problem. But the label and table need to be reusable, be two separate components.

So, I need two components and one call (to solve sync problem).
Thusly, I made a parent component for that one GET call and then send using Input() to Component 1 the label data and to Component 2 the table data.

Question
I had a big brainstorm thinking also about using SignalR and any other solution. This looks like a very common scenario, so I want to ask. What solution would you use?

Edited example to highlight impossibility to compute label data from table data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花落人离去 2025-01-19 19:47:26

有两种情况:

父子关系

如果 component-1 是父级,component-2 是其子级,那么您可以使用 < code>Outupt 装饰器定义一个 EventEmitter,一旦调用过滤器函数、获取结果并计算总和,就会发出该事件。

@Output() filterUsed = new EventEmitter();

filter(input) {
   ....
   this.sum = ....;
   this.filterUsed.emit(this.sum);
}

然后在父 component-1 标签中使用 Event-Binding component-2

及其内部getNewSum(sum)函数,更新父组件中sum的值;

不相关的组件

如果component-1component-2不相关,那么您可以添加中间件服务 将被注入到两个组件中。

该服务必须包含一个observablecomponent-2(接下来)将使用该observable来为component-1携带新的sum值(订阅)。

There're two cases:

parent-child relationship

if component-1 is parent and component-2 is its child, then you can use Outupt decorator to define an EventEmitter which will be emitted once filter function is called, results are fetched and sum is calculated.

@Output() filterUsed = new EventEmitter<number>();

filter(input) {
   ....
   this.sum = ....;
   this.filterUsed.emit(this.sum);
}

then in the parent component-1 use an Event-Binding in the tag of the component-2

<app-component-2 (filterUsed)="getNewSum($event)"></app-component-2>

and inside getNewSum(sum) function, update the value of the sum in the parent component;

not-related components

if component-1 and component-2 are not related, then you can add a middleware service that will be injected in both components.

this service must contain an observable that will be used by component-2 (next) to carry a new value of sum for the component-1 (subscribe).

肥爪爪 2025-01-19 19:47:25

假设您使用应用程序选择器以父子方式设置组件,则可以使用组件 2 的输出事件告诉组件 1 有关新总数的信息。

EG:

<component1>
    <component2 (totalChange)="totalChanged($event)"></component2>
</component1>

在子组件中,您需要声明一个输出事件发射器:

 @Output()
  totalChanged: EventEmitter<number> = new EventEmitter<number>();

然后在组件 2 中调用 api 后,当您获取新数据时,发出新的总计值:

// make API call and get new data
// emit event with new total of data
this.totalChanged.emit(newTotal);

然后在组件 1 中您可以添加一个名为“totalChanged”的函数:

public totalChanged(total: number){
    this.total = total;
}

Assuming you have your components set up in a parent child fashion with app-selectors, you can use an output event from component 2 to tell component 1 about the new total.

EG:

<component1>
    <component2 (totalChange)="totalChanged($event)"></component2>
</component1>

In the child component you need to declare an output event emitter:

 @Output()
  totalChanged: EventEmitter<number> = new EventEmitter<number>();

Then after your api call in component 2, when you get new data, emit the new total value:

// make API call and get new data
// emit event with new total of data
this.totalChanged.emit(newTotal);

Then in component1 you can add a function called 'totalChanged':

public totalChanged(total: number){
    this.total = total;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文