Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular 从角坐标系中分离的组件传递值_Angular - Fatal编程技术网

Angular 从角坐标系中分离的组件传递值

Angular 从角坐标系中分离的组件传递值,angular,Angular,我有两个组件。组件A和B。组件B有一个值,我正试图在组件A中重新接收该值。组件B位于与A不同的文件夹中。如何在不使用localStorage之类的东西的情况下将该值发送到A 这是我的上下文代码 成分B @Input() offerExpiry: any; async ngOnInit() { this._loader.start(); if (this._exploreService.offer) { ... } else { const

我有两个组件。组件A和B。组件B有一个值,我正试图在组件A中重新接收该值。组件B位于与A不同的文件夹中。如何在不使用localStorage之类的东西的情况下将该值发送到A

这是我的上下文代码

成分B

@Input() offerExpiry: any;

  async ngOnInit() {
    this._loader.start();
    if (this._exploreService.offer) {
        ...
    } else {
      const offer = await this._exploreService.getOfferDetails();
      //value I need is 'this.offerExpiry'
      this.offerExpiry = offer.expiryDate;
    }
    this._loader.stop();
  }
成分A

import { Component, Input, Output, EventEmitter } from '@angular/core';

@Component({
  selector: 'app-offer-subheader',
  templateUrl: './offer-subheader.component.html',
  styleUrls: ['./offer-subheader.component.scss']
})
export class OfferSubheaderComponent {

  @Input() title: string;
  @Input() active: string;
  @Output() back = new EventEmitter();
  @Output() offerExpiry: any;

  onBack() {
    this.back.emit();
  }

}

我尝试使用输入输出,但没有任何成功

您可以通过两种方式解决此问题:

  • 您需要创建多个父组件或使用您的应用程序 组件,组件B有@Output()和组件A@Input()。你 订阅组件B@输出更改并将值设置为父级 组件,并在组件A的输入上传递它
  • 创建如下服务:

    导出类交换服务{ 私有值$:BehaviorSubject=新的BehaviorSubject(1)


  • 并将其注入您的两个组件中,享受您的数据管理)

    检查此项(可能重复):是的,谢谢,我的一项已重复。将关闭。您可以检查此项的可能重复项-到目前为止,我正在将true从子项传递到子项。如果两者处于同一级别(而不是父项和子项),请使用此服务。
      get value() {
        return this.value$.asObservable();
      }
    
      updateValue(value: number) {
        this.value$.next(value);
      }
    }