Nodejs中的模块加载是否具有确定性?

时间:2019-04-10 21:53:33

标签: javascript node.js dependencies require circular-dependency

我正在尝试调试一些有几个循环依赖项的Node.js代码。这听起来像是一个问题,由于循环依赖而部分加载了模块(错误的形式均为<canvas id="myChart2" width="100%" height="35"></canvas> <script type="text/javascript"> let myChart2 = document.getElementById('myChart2').getContext('2d'); var aflTeamChart = new Chart(myChart2, { type: 'bar', data: { labels:[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18], datasets: [{},{}] }, //closes data options: { legend: {display: true}, maintainAspectRatio:true, responsive: true, hover: {mode: 'nearest', intersect: true}, scales: { xAxes: [{display: true, stacked:true, ticks: {fontSize: 10} }], yAxes: [{display: true, stacked:true, id:'left-axis', display: true, position: 'left', ticks: {beginAtZero: true} }] } } }); </script> <script type="text/javascript"> jQuery.ajax({ url: "/wp-admin/admin-ajax.php", dataType: "json", data: { action: 'call_php_code', }, success:function updateaflTeamChart(output){ aflTeamChart.data.datasets[0].data = [1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2,1,2]; aflTeamChart.data.datasets[0].backgroundColor = 'red'; aflTeamChart.data.datasets[0].label = 'data1'; aflTeamChart.data.datasets[1].data = [1,4,6,7,3,1,4,6,8,3,1,6,7,5,2,4,7,3]; aflTeamChart.data.datasets[1].backgroundColor = 'green'; aflTeamChart.data.datasets[1].label = 'data2'; aflTeamChart.update(); } }); </script> )。但是,错误很少出现,并且当它们确实出现时,会在代码的不同部分发生。因此,我无法以任何确定性的方式重现错误。

这使我相信模块加载顺序不是确定的,这与我已经阅读的某些现有材料(例如this article)相矛盾。就是这种情况,我所看到的问题会更有意义。

0 个答案:

没有答案