如何将类和函数中的一个变量导出到Angular中的另一个组件?
我有一个名为如何将类和函数中的一个变量导出到Angular中的另一个组件?,angular,typescript,variables,undefined,Angular,Typescript,Variables,Undefined,我有一个名为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;' },
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;
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() { }
}
代码运行良好,我可以在控制台console.log中看到这行代码的结果('RSI结果为:',RSI_结果)代码>但类外的导出
行似乎不起作用,我无法在其他组件中使用RSI_结果
,并获取未定义的RSI_结果
错误消息!另外,当我在控制台中写入rsi_结果
或rsi_结果
时,我会得到相同的未定义错误
当这行代码console.log('RSI结果为:',RSI_结果)出现时,怎么可能出现这种情况
运行良好,我可以在控制台中看到rsi_结果
,但当我手动键入时,会收到未定义的错误消息
伊迪尔
我试图创建一个名为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图表的组件中使用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;
}
现在程序运行得很好 您必须将您的共享信息
放在一个服务中,并将该服务注入到您的所有组件中
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();
}
}
更像是这样,请删除。但现在的问题是:为什么你认为这会起作用?它不是@Component
,因此它实现AfterViewInit
这一事实是不相关的,而且在类定义结束和您尝试导出结果之间,类中的任何方法都不会被调用。@jonrsharpe:我应该如何修复它?我不知道。我不知道你想达到什么目的。你的代码现在没有任何意义,很难想象你在尝试什么。好吧,但那没有任何意义。同样,在导出时,您从未实例化过该类,更不用说调用它的任何方法了。如果您想在类之间共享某些内容,请将其移动到服务:。@Hasani这不是它的工作方式,这不是任何一种工作方式;)myService
中的getRsi()
是什么?我尝试了你的方式,但没有成功!请阅读我问题的编辑部分。它起作用了!非常感谢你!