angular - 未捕获的 ReferenceError:在初始化之前无法访问“RSI_RESULT”
问题描述
我想要做的是使用函数创建一个数组setRsi()
,然后使用函数绘制它,该doOutput()
函数打开一个新对话框以使用ngg-charts
组件显示结果。
这是flow.component.ts
文件,setRsi()
方法是点击事件触发函数:
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);
}
doOutput(){
this.dialog.open(NgxChartsComponent ) ; //, {width: '500px', height: '500px'});
}
}
export const RSI_RESULT = rsi_result;
export const MACD_RESULT = macd_result;
doOutput()
也是导致错误消息的另一个点击事件函数。
单击相关按钮时,我可以在控制台中看到rsi_result
或macd_result
,但是当我想导出和导入然后在ngx-chart.component.ts
文件中绘制数据时,我会收到以下错误:
ngx-charts.component.ts
:
import { Component, OnInit } from '@angular/core';
import {MatDialog, MatDialogRef} from '@angular/material';
import {MACD_RESULT , RSI_RESULT} from '../flow/flow.component';
import {Mellat, Khodro, Shepna} from '../shared/stock_inf';
import { NewsComponent } from '../news/news.component';
var mellat = Mellat;
var khodro = Khodro;
var shepna = Shepna;
var macd_result = MACD_RESULT;
var rsi_result = RSI_RESULT;
console.log(rsi_result);
@Component({
selector: 'app-ngx-charts',
templateUrl: './ngx-charts.component.html',
styleUrls: ['./ngx-charts.component.scss']
})
export class NgxChartsComponent implements OnInit {
constructor(public dialogRef: MatDialogRef<NgxChartsComponent>) { }
// data goes here
public single = [
{
"name": " درصد سود",
"value": 69
},
{
"name": " درصد زیان",
"value": 31
},
];
// LENGTH = RSI_RESULT.length;
newArray = rsi_result.map((e, i) => ({
"name": (i + 1).toString(),
"value": e,
}));
newArray2 = mellat.map((e, i) => ({
"name": (i + 1).toString(),
"value": e,
}));
public multi = [
{
"name": "RSI",
"series": this.newArray
},
{
"name": "Mellat",
"series": this.newArray2
},
];
//{name: (i + 1).toString(), value: e.toString()}
view: any[] = [700, 400];
// 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;
ngOnInit() {
}
}
这是错误消息:
main.js:5431 Uncaught ReferenceError: Cannot access 'RSI_RESULT' before initialization
at Module.RSI_RESULT (main.js:5431)
at Module../src/app/ngx-charts/ngx-charts.component.ts (ngx-charts.component.ts:13)
at __webpack_require__ (bootstrap:79)
at Module../src/app/flow/flow.component.ts (main.js:5440)
at __webpack_require__ (bootstrap:79)
at Module../src/app/app.module.ts (app.component.ts:8)
at __webpack_require__ (bootstrap:79)
at Module../src/main.ts (main.ts:1)
at __webpack_require__ (bootstrap:79)
at Object.0 (main.ts:13)
我认为通过传递存在问题RSI_RESULT
,MACD_RESULT
并且我还收到以下警告:
client:126 [WDS] Warnings while compiling.
warnings @ client:126
(anonymous) @ socket.js:47
sock.onmessage @ SockJSClient.js:58
EventTarget.dispatchEvent @ sockjs.js:170
(anonymous) @ sockjs.js:887
SockJS._transportMessage @ sockjs.js:885
EventEmitter.emit @ sockjs.js:86
WebSocketTransport.ws.onmessage @ sockjs.js:2961
wrapFn @ zone-evergreen.js:1191
invokeTask @ zone-evergreen.js:391
runTask @ zone-evergreen.js:168
invokeTask @ zone-evergreen.js:465
invokeTask @ zone-evergreen.js:1603
globalZoneAwareCallback @ zone-evergreen.js:1629
client:135 Circular dependency detected:
src\app\flow\flow.component.ts -> src\app\ngx-charts\ngx-charts.component.ts -> src\app\flow\flow.component.ts
warnings @ client:135
(anonymous) @ socket.js:47
sock.onmessage @ SockJSClient.js:58
EventTarget.dispatchEvent @ sockjs.js:170
(anonymous) @ sockjs.js:887
SockJS._transportMessage @ sockjs.js:885
EventEmitter.emit @ sockjs.js:86
WebSocketTransport.ws.onmessage @ sockjs.js:2961
wrapFn @ zone-evergreen.js:1191
invokeTask @ zone-evergreen.js:391
runTask @ zone-evergreen.js:168
invokeTask @ zone-evergreen.js:465
invokeTask @ zone-evergreen.js:1603
globalZoneAwareCallback @ zone-evergreen.js:1629
client:135 Circular dependency detected:
src\app\ngx-charts\ngx-charts.component.ts -> src\app\flow\flow.component.ts -> src\app\ngx-charts\ngx-charts.component.ts
编辑:
如果我将变量放在我的类中,如下所示:
export class NgxChartsComponent implements OnInit {
mellat = Mellat;
khodro = Khodro;
shepna = Shepna;
macd_result = MACD_RESULT;
rsi_result = RSI_RESULT;
...
}
我将收到以下新错误:
ERROR TypeError: Cannot read property 'map' of undefined
at new NgxChartsComponent (ngx-charts.component.ts:45)
at createClass (core.js:27865)
at createDirectiveInstance (core.js:27685)
at createViewNodes (core.js:38315)
at createRootView (core.js:38187)
at callWithDebugContext (core.js:39716)
at Object.debugCreateRootView [as createRootView] (core.js:38953)
at ComponentFactory_.create (core.js:26827)
at ComponentFactoryBoundToModule.create (core.js:22791)
at ViewContainerRef_.createComponent (core.js:26983)
View_NgxChartsComponent_Host_0 @ NgxChartsComponent_Host.ngfactory.js? [sm]:1
proxyClass @ compiler.js:19436
logError @ core.js:39651
handleError @ core.js:9162
dispatchEvent @ core.js:25822
(anonymous) @ core.js:37030
(anonymous) @ platform-browser.js:1789
invokeTask @ zone-evergreen.js:391
onInvokeTask @ core.js:34182
invokeTask @ zone-evergreen.js:390
runTask @ zone-evergreen.js:168
invokeTask @ zone-evergreen.js:465
invokeTask @ zone-evergreen.js:1603
globalZoneAwareCallback @ zone-evergreen.js:1629
NgxChartsComponent_Host.ngfactory.js? [sm]:1 ERROR CONTEXT DebugContext_ {view: {…}, nodeIndex: 1, nodeDef: {…}, elDef: {…}, elView: {…}}
解决方案
你将不得不把你所有的变量
var mellat = Mellat;
var shepna = Shepna;
var macd_result = MACD_RESULT;
var rsi_result = RSI_RESULT;
在导出类块内不使用 var 关键字,如下所示。
export class NgxChartsComponent implements OnInit {
mellat = Mellat;
shepna = Shepna;
macd_result = MACD_RESULT;
rsi_result = RSI_RESULT;
constructor(public dialogRef: MatDialogRef<NgxChartsComponent>) { }
}
因为使用 var 会创建一个全局变量,你不会在“this”对象中获得它
推荐阅读
- java - 如何识别 JFR 事件类型
- photoshop - 如何在 Photoshop 中编写间隔脚本
- css - 如何在闪亮的应用程序中更改使用 DT 库生成的表格标题的颜色?
- reactjs - 如何用纱线解决依赖项安装问题?
- node.js - 我正在创建 appInsights 的 NPM 包。即使是从配置传递的密钥仍然出现错误
- css - 如何仅在 wordpress 中使用 css 使响应式菜单上的子项始终对移动设备可见
- python-3.x - 如何使用 Python 的 Kubernetes 库删除节点污点
- android - 从 Git 克隆时找不到点 JKS(.jks 文件)
- node.js - 配置 elastic beanstalk 以安装开发工具
- postgresql - 调用 prostresql 过程 nestjs/typeorm 时出错