美文网首页
electron中如何共享数据--global

electron中如何共享数据--global

作者: 前端青音 | 来源:发表于2020-02-24 00:05 被阅读0次

1、localstorage,sessionstorage依然可以用在electron中

2、在electron中,可以在主进y程中添加一个全局变量,然后在渲染进程中通过remote模块引入

主进程中,定义的全局变量‘shareObject’(这个名字可以随便取)
main.js

global.shareObject = {
   name:'我是那个共享数据'
}

渲染进程,通过remote模块的getGlobal方法可以获得我们在主进程中定义的全局变量
render.js

const remote = require('electron').remote
// 1、获取
let res = remote.getGlobal('shareObject').name
console.log(res) // 打印‘我是那个共享数据’

// 2、也可以改变这个数据
remote.getGlobal('shareObject').name = '我是重新被定义后的共享数据'


相关文章

网友评论

      本文标题:electron中如何共享数据--global

      本文链接:https://www.haomeiwen.com/subject/yltyqhtx.html