main.js
//全局数据
global.constantObject = {
name:"aaa"
};
a.js
const remote = require('electron').remote;
let btna = document.querySelector('#btna');
btna.onclick = function(event,msg){
console.log("btna"+msg);
console.log(remote.getGlobal('constantObject').name);
console.log("改变数据");
remote.getGlobal('constantObject').name = "cccc";
};
b.js
const remote = require('electron').remote;
let btnb = document.querySelector('#btnb');
btnb.onclick = function(event,msg){
console.log("btnb"+msg);
console.log(remote.getGlobal('constantObject').name);
};
index.html
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>news</title>
</head>
<body>
<h1>hello</h1>
<input id="btn" type="button" value="点我啊">
<input id="btna" type="button" value="btna">
<input id="btnb" type="button" value="btnb">
<!--1 引入render.js -->
<!-- <script src="./render-process/render.js" ></script> -->
<!-- electron 基于node -->
<script>
require('./render-process/render.js')
require('./render-process/a.js');
require('./render-process/b.js');
</script>
<!-- 区别 require 引入的文件的最外层的变量不是全局变量 src引入的是全局变量 -->
</body>
</html>
本文介绍在Electron框架下,如何通过main.js设置全局数据,并在不同模块如a.js和b.js中访问和修改这些数据。演示了使用remote模块获取全局常量对象并更新其属性的过程。
1800

被折叠的 条评论
为什么被折叠?



