如何将 class 中的变量导出到 Angular 中的另一个组件?

How to export a variable withinin a class and function to another component in Angular?

我有一个名为 flow.component.ts 的组件,如下所示:

var rsi_result: number[];

@Component({
  selector: 'flow-home',
  templateUrl: './flow.component.html',
  styleUrls: ['./flow.component.scss'],
  host: {
    '[@flyInOut]': 'true',
    'style': 'display: block;'
  },
  animations: [
    flyInOut(),
    expand()
  ]
})
export class FlowComponent implements AfterViewInit {

  setRsi(selectedValue){
    console.log('The RSI period is:' , selectedValue);
    periodd = selectedValue;
    this.label2 = ' نام اندیکاتور:  RSI';
    this.label3 = ' دوره زمانی: ' + periodd + 'روزه';
    rsi_result = rsi({period: periodd, values: pricess});
    console.log('The RSI result is:' ,  rsi_result);
  }

}

export const RSI_RESULT = rsi_result;

代码运行良好,我可以在我的控制台 console.log('The RSI result is:' , rsi_result); 中看到这行代码的结果,但似乎 class 中的 export 行不起作用而且我无法在其他组件中使用 RSI_RESULT 并收到 undefined RSI_RESULT 错误消息!另外,当我在控制台中写入 rsi_resultRSI_RESULT 时,我得到了同样的未定义错误!

当这行代码 console.log('The RSI result is:' , rsi_result); 运行良好并且我可以在控制台中看到 rsi_result 但是当我手动输入它时我收到未定义的错误消息怎么可能?

编辑

我尝试使用以下内容创建名为 ngx.service 的服务:

import { Injectable } from '@angular/core';

var RSI_RESULT: number[];
var MACD_RESULT: number[];

@Injectable({
  providedIn: 'root'
})
export class NgxService {

getRsiResult(){ 

  return (RSI_RESULT);
}  

getMacdResult(){ 
  return (MACD_RESULT);

}

  constructor() { }

}

然后将上面的代码编辑为:

export class FlowComponent implements AfterViewInit {

  macd_result: number[];
  rsi_result: number[];
  constructor(private dialog: MatDialog, private ngxService:NgxService) {
  }
  setRsi(selectedValue){
    console.log('The RSI period is:' , selectedValue);
    periodd = selectedValue;
    this.label2 = ' نام اندیکاتور:  RSI';
    this.label3 = ' دوره زمانی: ' + periodd + 'روزه';
    this.rsi_result = rsi({period: periodd, values: pricess});
    console.log('The RSI result is:' ,  this.rsi_result);
  }

}

但是当我想在以下名为 ngx-charts 的组件中使用 rsi_result 时,我得到了与以前相同的错误:

import { Component, OnInit } from '@angular/core';
import {MatDialog, MatDialogRef} from '@angular/material';
import { NewsComponent } from '../news/news.component';
import {NgxService} from '../services/ngx.service';
import { Mellat , Khodro, Shepna} from '../shared/stock_inf';


@Component({
  selector: 'app-ngx-charts',
  templateUrl: './ngx-charts.component.html',
  styleUrls: ['./ngx-charts.component.scss']
})



export class NgxChartsComponent implements OnInit {

  rsi_result: number[];
  macd_result: number[];

  constructor(public dialogRef: MatDialogRef<NgxChartsComponent> ,
     private ngxService: NgxService) { }

     ngOnInit() {
      this.rsi_result = this.ngxService.getRsiResult(); //.subscribe(RSI_RESULT => this.rsi_result = RSI_RESULT);
      this.macd_result = this.ngxService.getMacdResult(); //.subscribe(MACD_RESULT => this.macd_result = MACD_RESULT);

      console.log(this.rsi_result); 
    }

  // data goes here
public single = [
  {
    "name": " درصد سود",
    "value": 69
  },
  {
    "name": " درصد زیان",
    "value": 31
  },

];


 newArray = Mellat.map((e, i) => ({
  "name": (i + 1).toString(),
  "value": e,
}));

newArray2 = this.rsi_result.map((e, i) => ({
  "name": (i + 1).toString(),
  "value": e,
}));


public multi = [
  {
    "name": "Mellat",
    "series": this.newArray
  },

  {
    "name": "RSI",
    "series": this.newArray2
  }
];

//{name: (i + 1).toString(), value: e.toString()}



  view: any[];

  // options for the chart
  showXAxis = true;
  showYAxis = true;
  gradient = false;
  showLegend = true;
  showXAxisLabel = true;
  xAxisLabel = 'زمان';
  showYAxisLabel = true;
  yAxisLabel = 'قیمت';
  timeline = true;

  colorScheme = {
    domain: ['#5AA454', '#A10A28', '#C7B42C', '#AAAAAA']
  };

  // line, area
  autoScale = true;

  //pie
  showLabels = true;
  explodeSlices = false;
  doughnut = false;


}

编辑 2:

我再次检查了我的代码并注意到我必须从我的应用程序中删除 var RSI_RESULT: number[];MACD_RESULT: number[]; 并且当我的 ngx.service.ts 文件看起来像这样:

import { Injectable } from '@angular/core';
import {Observable, of} from 'rxjs';



@Injectable({
  providedIn: 'root'
})
export class NgxService {

RSI_RESULT: number[];
MACD_RESULT: number[];

getRsiResult(){ //: Observable<number[]>{

  return (this.RSI_RESULT);
}  

getMacdResult(){ //: Observable<number[]>{
  return (this.MACD_RESULT);

}

  constructor() { }

}

程序现在运行良好!

你必须将你的 share info 放在一个服务中,并将该服务注入到你的所有组件中。

export class FlowComponent implements AfterViewInit {

  constructor(private readonly myService: MyService){}

  setRsi(selectedValue){
    console.log('The RSI period is:' , selectedValue);
    periodd = selectedValue;
    this.label2 = ' نام اندیکاتور:  RSI';
    this.label3 = ' دوره زمانی: ' + periodd + 'روزه';
    this.myService.setRsi(rsi({period: periodd, values: pricess}));
    console.log('The RSI result is:' ,  rsi_result);
  }

}




// Other file
export class OtherComponent {

  constructor(private readonly myService: MyService){}

  getRsi(){
     return this.myService.getRsi();
   }

}