【问题标题】:Why is require('fs') causing my js variables to not show in html?为什么 require('fs') 导致我的 js 变量不在 html 中显示?
【发布时间】:2019-12-01 08:28:14
【问题描述】:

我正在尝试制作一个可以增加和减少数字并将该数字更新到本地 .txt 文件的电子应用程序。

我有显示和向上/向下计数的数字,但是一旦我引入 fs 节点,它就会恢复为“ScoreA”的占位符文本

var fs = require('fs'); 

//Score A initialise
var scoreAOriginal = 0;
var dx = scoreAOriginal;

//Score A button functions
function incA(num){
  dx = dx + num;
}

function decA(num){
    if (dx >= 1){
     dx = dx - num; 
    }
}

//Score B initialise
var scoreBOriginal = 0;
var dy = scoreBOriginal;

//Score B button functions
function incB(num){
    dy = dy + num;
}

function decB(num){
    dy = dy - num;
}

// Refresher and value assignment to div classes in index.html
setInterval(function(){ document.getElementById("scoreA").innerHTML = dx; document.getElementById("scoreB").innerHTML = dy; }, 100);
<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Peppy - by Doug</title>
    <script src="./main.js"></script>
    <link rel="stylesheet" type="text/css" href="main.css">
  </head>
  <body>
    <h1>Peppy</h1>
      <div class="scoreConsoles">
        <div>
          <button class="button button5" onclick="incA(1)">+</button>
            <div id="scoreA">scoreA</div>
          <button class="button button5" onclick="decA(1)">-</button>
        </div>
        <div>
          <button class="button button5" onclick="incB(1)">+</button>
            <div id="scoreB">scoreB</div>
          <button class="button button5" onclick="decB(1)">-</button>
        </div>
      </div>
    <!-- You can also require other files to run in this process -->
    <script src="./renderer.js"></script>
  </body>
</html>

【问题讨论】:

  • 可能是因为require 没有定义(在浏览器中;'fs' 在浏览器中肯定不可用)并且脚本因错误而停止。
  • 添加你写的完整的html代码
  • @Piyush,添加了完整的 html

标签: javascript html fs


【解决方案1】:

您是如何创建窗口的?您知道您需要启用节点集成:

const mainWindow = new BrowserWindow({
    webPreferences: {
        nodeIntegration: true
    }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多