简体   繁体   English

如何在回调中停止无限递归

[英]How to stop infinite recursion in callback

I've been trying to understand why and how to fix this infinite recursion happening in the legendCallback Vue's Vue-charts to no avail. 我一直在试图理解为什么以及如何解决在legendCallback发生的无限递归legendCallback Vue的Vue图表无济于事。 Help is much appreciated. 非常感谢您的帮助。

In my Vue component, options is defined in methods as: 在我的Vue组件中,选项在方法中定义为:

  options() { var self = this; return { legend: { display: false, position: 'top', }, legendCallback: chart=> { return this.generateLegendData(chart) }, tooltips: { mode: 'single', callbacks: { label: function (tooltipItems) { return "text" } }, } } }, 

And my generateLegendData method looks like this: 我的generateLegendData方法如下所示:

  generateLegendData(chart) { var labels = ["SolarCity", "Einstein", "SpaceX", "Mars", "Tesla"]; var backgroundColor = [ "rgba(242,58,48, 0.1)", "rgba(110,75,63,1)", "rgba(55,72,172,1)", "rgba(0,39,39,1)", "rgba(166,176,69,1)" ]; var text = []; text.push('<ul class="' + chart.id + '-legend">'); for (var i = 0; i < labels.length; i++) { text.push( '<li><span style="background-color:' + backgroundColor[i] + '">' ); if (labels[i]) { text.push(labels[i]); console.log(labels[i]); } text.push("</span></li>"); } text.push("</ul>"); return text.join(""); } 

Without the for loop, ie just returning the expected string it works fine. 如果没有for循环,即仅返回期望的字符串,它就可以正常工作。

Returning return text.join("") was not the problem. 返回return text.join("")并不是问题。

The solution is moving options property from a method to a computed property. 解决方案是将options属性从方法移动到计算属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM