简体   繁体   English

从Angular 2+中的同级组件访问元素

[英]Accessing an element from sibling component in Angular 2+

I have a set of components that work together. 我有一组可以协同工作的组件。 Basically I am displaying a list of labels, and a list of data for each label. 基本上,我正在显示标签列表以及每个标签的数据列表。 Since the label pane is resizable, it becomes a sibling of the data component. 由于标签窗格可调整大小,因此它成为数据组件的同级对象。 Ie- my list component looks like: 即,我的列表组件如下所示:

<app-list-labels></app-list-labels>
<app-list-data></app-list-data>

And list-labels and list-data look like this, respectively: 列表标签和列表数据分别如下所示:

// app-list-labels:
<div class="label-header">Labels</div>
<div class="label-labels" id="labels-labels">
   <!-- all of my labels looped over and displayed -->
</div>

// app-list-data:
<div class="data-header">Labels</div>
<div class="data-data" id="data-data">
   <!-- all of my data rows looped over and displayed -->
</div>

Both labels-labels and data-data have overflow-y set to auto, so that they can scroll if the number of rows exceeds the container size. labels-labelsdata-data都将溢出-y设置为auto,因此如果行数超过容器大小,它们可以滚动。 The number and size of rows between the two are always identical. 两者之间的行数和大小始终相同。 My goal is to have both containers scroll if one container is being scrolled. 我的目标是如果一个容器正在滚动,则两个容器都滚动。 So I'd need to attach a (scroll) event listener to both of those divs ( #data-data and #labels-labels ), and update the scroll value of the non-scrolled element. 因此,我需要将(scroll)事件侦听器附加到这两个div( #data-data#labels-labels ),并更新非滚动元素的滚动值。 My problem is- how can I access an element from one component in a sibling component? 我的问题是-如何从同级组件中的一个组件访问元素? If app-labels was embedded in app-data it would be straight forward, but being siblings, I cant see how to do it. 如果将app-labels嵌入app-data那将是直接的,但是是兄弟姐妹,我看不到该怎么做。

You could try exposing the Div's using @Output decoratos, like this: 您可以尝试使用@Output decoratos公开Div的内容,如下所示:

<app-component-one (divComponent)="divOne = $event"></app-component-one>
<app-component-two (divComponent)="divTwo = $event"></app-component-two>

Sibling 1: 兄弟1:

import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core';

@Component({
  selector: 'app-component-one',
  templateUrl: './component-one.component.html',
  styleUrls: ['./component-one.component.css']
})
export class ComponentOneComponent implements OnInit, AfterViewInit {

  @ViewChild('divComponent1') divComponent1: ElementRef;
  @Output() divComponent = new EventEmitter();

  constructor() {
  }

  ngOnInit() {
  }

  ngAfterViewInit(): void {
    this.divComponent.emit(this.divComponent1);
  }


}

Sibling 2: 兄弟2:

import { AfterViewInit, Component, ElementRef, EventEmitter, OnInit, Output, ViewChild } from '@angular/core';

@Component({
  selector: 'app-component-two',
  templateUrl: './component-two.component.html',
  styleUrls: ['./component-two.component.css']
})
export class ComponentTwoComponent implements OnInit, AfterViewInit {

  @ViewChild('divComponent2') divComponent1: ElementRef;
  @Output() divComponent = new EventEmitter();

  constructor() {
  }

  ngOnInit() {
  }

  ngAfterViewInit(): void {
    this.divComponent.emit(this.divComponent1);
  }

}

Parent Component, the one that has the siblings in : 父组件,具有以下组件的同级组件:

import { AfterViewInit, Component, ElementRef, OnInit } from '@angular/core';

@Component({
  selector: 'app-component-base',
  templateUrl: './component-base.component.html',
  styleUrls: ['./component-base.component.css']
})
export class ComponentBaseComponent implements OnInit, AfterViewInit {

  divOne: ElementRef;
  divTwo: ElementRef;

  constructor() {
  }

  ngOnInit() {
  }

  ngAfterViewInit(): void {
    console.log('div one' , this.divOne);
    console.log('div two' , this.divTwo);
  }


}

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

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