首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个相同级别组件之间的交换引用。

两个相同级别组件之间的交换引用。
EN

Stack Overflow用户
提问于 2020-05-29 10:37:09
回答 2查看 295关注 0票数 0

我有两个custom-component,它们是由一个具有不同数据的公共父级生成的,作为mat-tab-group上的两个选项卡。

代码语言:javascript
复制
<mat-tab-group>
  <mat-tab label="TAB1">
    <ng-template matTabContent>
      <custom-component [data]="tab1data"></custom-component>
    </ng-template>
  </mat-tab>
  <mat-tab label="TAB2">
    <ng-template matTabContent>
      <custom-component [data]="tab2data"></custom-component>
    </ng-template>
  </mat-tab>
</mat-tab-group>

data是一个设置内部_data并将其封装在MatTableDataSource中的设置器:

代码语言:javascript
复制
@Input()
set data(val: Data[]) {
    this._data = val;
    this.loadData();
}

loadData(): void {
    this.dataSource = new MatTableDataSource<Data>(this._data);
    this.dataSource.sort = this.sort;
}

在第一个选项卡上,组件的操作应该会影响到其他选项卡上的数据。是否有任何方法传递组件引用,以便更改_data并从其他组件调用loadData()

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-06-03 07:08:18

您可以使用Rxjs可观察到的数据来完成这一任务。

下面是如何在两个组件https://stackblitz.com/edit/angular-ivy-myr2kh之间进行通信的stackblitz示例

my-service.service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

@Injectable()
export class MyServiceService {
  private dataChangeObservable = new Subject<any>();
    dataChangeStream = this.dataChangeObservable.asObservable();

  constructor() { }

  emitDataChange() {
    this.dataChangeObservable.next();
  }
}

ComponentOne.component.ts

代码语言:javascript
复制
onclickDiv() {
    this.myService.emitDataChange(); // Here you are triggering for change
}

ComponentTwo.component.ts

代码语言:javascript
复制
ngOnInit() {
    this.dataChangeSubscription$ = this.myService.dataChangeStream.subscribe(() => {
       this.count++; // Here you will get notified/listener of change
    })
 }

更新

如果您有相同的组件实例,那么您必须传递一些值来确定应该更新哪个实例。

喜欢

https://stackblitz.com/edit/angular-ivy-4pznor

您的父html

代码语言:javascript
复制
<app-componentone [name]="'one'"></app-componentone>
<app-componentone [name]="'two'"></app-componentone>

在这里,onetwo作为输入传递来标识实例

然后你的ts

代码语言:javascript
复制
import { Component, OnInit, Input } from '@angular/core';
import { MyServiceService } from '../my-service.service';

@Component({
   selector: 'app-componentone',
   templateUrl: './componentone.component.html',
   styleUrls: ['./componentone.component.css']
})
export class ComponentoneComponent implements OnInit {
  @Input() name; // it will have instance name
  count = 0;
  constructor(
     private myService: MyServiceService
  ) { }

  ngOnInit() {
    this.myService.dataChangeStream.subscribe((value) => { // here we will get to notify which instance should get updated
      if (this.name !== value) { // Here we checking for instance name for updating, if same component instance don't do anything else update
         this.count++;
      }
    })
  }

  onclickDiv() {
    // Here I am passing parameter, so if click is triggered from instance one, we have to update other instances, so passing parameter 'one' i.e. name, to avoid updating same component instance

    this.myService.emitDataChange(this.name);
  }
}
票数 1
EN

Stack Overflow用户

发布于 2020-06-03 08:47:33

从第一个选项卡,您可以在dataChange上向其父组件发出输出。

在自定义组件中,有一个输出,如

代码语言:javascript
复制
@Output() public dataChange = new EventEmitter();

在数据更改时,发出此事件如下:

代码语言:javascript
复制
this.dataChange.emit('changed data');

在此组件的选择器所在的父组件中,将选择器更改为:

HTML :-

代码语言:javascript
复制
<custom-component [data]="tab1data" (dataChange)="modifyDataTab2($event)"></custom-component>

TS :-

代码语言:javascript
复制
modifyDataTab2(firstTabData) {
   this.tab2Data = firstTabData;
   // or you can make other changes.
} 

现在,由于tab2已经将tab2Data作为输入,您的loadData()将自动被调用。

如果要修改选项卡2上的tab1数据,则不需要再次更改选项卡组件,每个实例都会有自己的输出。更改将只发生在父级。这将是:

HTML :-

代码语言:javascript
复制
<custom-component [data]="tab2data" (dataChange)="modifyDataTab1($event)"></custom-component>

TS :-

代码语言:javascript
复制
modifyDataTab1(secondTabData) {
   this.tab1Data = secondTabData;
   // or you can make other changes.
} 

这就是有一个共同的父母的好处。如果两个孩子想要沟通,他们应该通过父母进行交流。因为父类中作为输入传递的任何数据更改都将自动触发子元素所需的更改。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62084015

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档