即时通讯使用chart.js创建图表。我在这里有3个功能dataSurvey
我用它从数据库中返回我的数据,并用createBarChart
生成图表,然后用handle
处理onclick图表,我想在用户单击图表时显示一些信息。
我的变量和加载函数
dataSurveys:any
ionViewDidEnter() {
this.dataSurvey()
}
我的
dataSurvey
代码dataSurvey() {
this.api.get('product/getsurvey/'+this.CekLogin.data.id)
.subscribe((result:any) => {
this.dataSurveys = result.data
if (this.dataSurveys) {
this.createBarChart()
}
})
}
我的
createBarChart
代码createBarChart() {
this.bars = new Chart(this.barChart.nativeElement, {
type: 'pie',
data: {
labels: this.dataSurveys.prodName,
datasets: [{
data: this.dataSurveys.prodScore,
backgroundColor: this.dataSurveys.prodColor,
borderColor: 'rgb(38, 194, 129)',
borderWidth: 1
}]
},
options: {
onClick: this.handle
}
});
}
和我的
handle
代码handle(point, event) {
let item = event[0]
console.log(item)
console.log(this.dataSurveys)
}
我尝试在
console.log(this.dataSurveys)
和dataSurvey
函数上使用此命令createBarChart
,它返回数据,但是当我从handle
函数运行它时,它返回我“未定义”。我该如何解决这个问题?
最佳答案
在this
方法的上下文中,很可能handle
是别的东西...不是您的Ionic Page。您可以console.log(this)
并查看其实际含义。
如果您实际上需要使用页面中的变量逻辑,则可以使用JS方法apply。基本上它将用您需要的上下文替换上下文。
options: {
onClick: this.handle.apply(this)
}
在此处查看更多信息https://www.w3schools.com/js/js_function_apply.asp
关于javascript - ionic 不从变量返回数据,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59156469/