【发布时间】:2019-12-06 07:53:11
【问题描述】:
我是 Electron 和 JS 的新手。
我已经寻找一种解决方案来创建一个执行.bat 文件或.exe 文件的简单按钮。
我已经阅读了article 关于使用child_process 的内容。
但是,它没有说明如何将 var“链接”到我的按钮。
我的代码写在renderer.js
【问题讨论】:
标签: javascript html node.js electron
我是 Electron 和 JS 的新手。
我已经寻找一种解决方案来创建一个执行.bat 文件或.exe 文件的简单按钮。
我已经阅读了article 关于使用child_process 的内容。
但是,它没有说明如何将 var“链接”到我的按钮。
我的代码写在renderer.js
【问题讨论】:
标签: javascript html node.js electron
electron 使用 nodejs 运行,因此您可以执行以下操作:
var execFile = require('child_process').execFile;
var runExe = function(){
execFile('<your-name>.exe', function(err, data) {
console.log(err)
console.log(data.toString());
});
}
现在打电话
runExe()
使用你的按钮,你应该很高兴
更多信息请看这里 node js reference
所以发生的情况基本上是我们使用你已经说过的 nodejs child_process 运行指定的 exe 文件...希望有所帮助
【讨论】:
好的,现在是我发布电子解决方案的工作:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
Chromium <script>document.write(process.versions.chrome)</script>,
and Electron <script>document.write(process.versions.electron)</script>.
<h1> A simple Javascript created button </h1>
<button onclick="function()">Firefox 1</button>
<button onclick="firefox()">Firefox 2</button>
<script>
var execFile = require('child_process').execFile;
function firefox(){
execFile("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe", function(err, data) {
console.log(err)
console.log(data.toString());
});
}
// You can also require other files to run in this process
require('./renderer.js')
</script>
</body>
</html>
【讨论】: