繁体   English   中英

如何将数组传递给另一个组件?

[英]How can I pass an array to another component?

我有字符串数组 FidId,我需要传递这个数组以在另一个组件中使用

 public AddSelection(layer: VectorLayer, map:Map){
    this.vectorSource = layer;
      //start select
      var FidId:string[] = [];
      var FeatureFind = false;
      var select = new Select();

      map.addInteraction(select);
      var selectedFeature = select.getFeatures();

      this.dragBox = new DragBox({
        condition:(condition as any).platformModifierKeyOnly
      });
      map.addInteraction(this.dragBox);

      this.dragBox.on('boxend', () => {
        var extent = this.dragBox.getGeometry().getExtent();
        this.vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {
          FeatureFind = true;
          selectedFeature.push(feature);
          FidId.push(feature.get('id'));
        });

        if(FeatureFind)
        this.dialog.open(DialogData2Component);
        
      });
      this.dragBox.on('boxstart',()=>{
        selectedFeature.clear();
        FeatureFind = false;
        FidId = [];
      });
      //end select

  }

这是我需要它的地方

细节

我是 angular 的新手,所以请帮忙:)

我处理将任何 arguments 从我的孩子传递给父母的方式(在我看来这就是你需要的)

首先在我的子组件中定义我的 eventEmitter 并初始化我想传递给我的父级的数组

//child component.ts
array: any[] = [];
@Output() passArray = new Eventemitter

然后我写一个 function 调用我的事件发射器并像这样发出我的数组

// child component.ts
emitArray() {
  this.passArray.emit(array);
}

现在我将 go 放入我的父组件的 HTML 中,我的子组件位于此处,我可以通过将它放入这样的括号中来收听 eventEmitter

// parent component.html
<custom-component (passArray)="yourFunction($event)"><custom-component>

// parent component.ts
yourFunction(parameter) {
  do.something()
}

所以一旦 passArray.emit 被称为我们的父组件,它在 html 中监听它,执行“yourFunction”,其中包含从 $event 获取的参数。 $event 是我们在子组件中传递的数据。 有关如何在 angular 中传递数据的更多详细信息,您可以通读https://angular.io/docs上的文档。 他们很好地解释了许多主题:)(比我好:D)

这是我的第一个答案之一,如果有点乱,那就太抱歉了:)我希望我能提供帮助;)

暂无
暂无

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

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