繁体   English   中英

ejs文件中的外部js function

[英]External js function in ejs file

我有home.ejs文件,我需要在我的页面上添加字符,所以我将外部 js 文件添加到我的代码的页脚部分:

<script src="demo/superuser-chart.js"></script>

我在 superuser-chart.js 文件中添加了我的图表,如果我在没有 function 的情况下使用它,它可以工作并显示图表。 但是当我在 function 中使用并从 home.ejs 调用时它无法工作。 这是来自superuser-chart.js文件的 function 图表:

   function chartIt(){
  console.log("chart calling")
// Area Chart Example
var ctx = document.getElementById("superUserChart");
const myChart = new Chart(ctx, {
  type: 'bar',
  data: {
      labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
      datasets: [{
          label: '# of Votes',
          data: [12, 19, 3, 5, 2, 3],
          backgroundColor: [
              'rgba(255, 99, 132, 0.2)',
              'rgba(54, 162, 235, 0.2)',
              'rgba(255, 206, 86, 0.2)',
              'rgba(75, 192, 192, 0.2)',
              'rgba(153, 102, 255, 0.2)',
              'rgba(255, 159, 64, 0.2)'
          ],
          borderColor: [
              'rgba(255, 99, 132, 1)',
              'rgba(54, 162, 235, 1)',
              'rgba(255, 206, 86, 1)',
              'rgba(75, 192, 192, 1)',
              'rgba(153, 102, 255, 1)',
              'rgba(255, 159, 64, 1)'
          ],
          borderWidth: 1
      }]
  },
  options: {
      scales: {
          y: {
              beginAtZero: true
          }
      }
  }
});

}

我这样称呼 home js:

图表它()

它不起作用,我也看不到console.log output。 当我从浏览器检查控制台错误日志时,我收到此错误: Uncaught ReferenceError: chartIt is not defined我该如何解决这个问题?

Uncaught ReferenceError: chartIt is not defined表示您在加载<script src="demo/superuser-chart.js"></script>之前调用了 function。您需要先加载脚本,然后再调用 function。添加<script src="demo/superuser-chart.js"></script>在 header 中调用 function

暂无
暂无

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

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