【问题标题】:Electron 'window' is not defined电子“窗口”未定义
【发布时间】:2019-11-28 16:59:59
【问题描述】:

我正在尝试构建一个使用 Google Charts api 绘制折线图的应用。 我已经设置的一件事是窗口上的“调整大小”事件的事件侦听器。这会触发折线图重新绘制,并将尺寸设置为“window.innerWidth”和“window.innerHeight”以适应新的窗口大小。

如果我使用 npm start 运行应用程序,这一切都很好。问题是当我使用电子打包器构建应用程序时。运行它构建的 exe 会抛出这个:

这是我的 HTML:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>AP Test Viewer</title>
    <link rel="stylesheet" href="css/styles.css">

  </head>
  <body ng-app="ApApp" ng-controller="MainController">

    <a ui-sref="single">Single</a>
    <a ui-sref="multi">Multi</a>

    <ui-view></ui-view>


  </body>

  <script>
    // You can also require other files to run in this process
    require('./renderer.js');
    require('./js/loader.js'); //google charts loader.js
    require('./js/app.js');
  </script>
</html>

我的应用程序的相关部分,我在这里使用窗口:

app.fun.drawGraph = function(){
    if(app.data.graphConfig){
        app.data.graphConfig.options.width = window.innerWidth * 0.97;
        app.data.graphConfig.options.height = window.innerHeight * 0.90;

        var chart = new google.visualization.LineChart(document.getElementById('chart'));
        chart.draw(app.data.graphConfig.data, app.data.graphConfig.options);
        // chart.draw($scope.graphData, google.charts.Line.convertOptions(options));    
    }
}

window.addEventListener('resize', function(e){
    if(app.data.graphConfig){
        app.fun.drawGraph();
    }
})

据我所知,这应该只是“窗口”,可以通过网页上的 javascript 访问。

【问题讨论】:

  • 你解决了吗?我面临着同样的问题。

标签: electron


【解决方案1】:

看起来您引用的相关 Javascript 正在主进程中运行,而不是在渲染器进程中运行。如果您将引用 window 的代码移动到 html 中的脚本标记中,您应该不会看到同样的问题。

就需要来自 html 页面的文件而言,我希望它也能像在脚本标记中一样工作。不过我可能弄错了。

【讨论】:

    【解决方案2】:

    我遇到了这个问题,发现我的package.json 没有定义main,所以Electron 认为index.js 是运行Electron 主进程的脚本。

    main 设置为专用脚本文件(我的情况为electron.js)后,一切都很好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-18
      • 2017-10-01
      • 2021-10-06
      • 2019-06-03
      • 2021-01-15
      • 2019-10-12
      相关资源
      最近更新 更多