WEB/Electron

[Electron] Global Variable 사용 방법

S0PH1A 2019. 4. 5. 12:08
반응형

[Electron] Global Variable 사용 방법


main 과 renderer 에서 변수나 함수를 공유하기 위해서는

ipcMain, ipcRenderer 을 사용하면 되지만,

Global Variable을 정의하여 사용하는 방법도 있다.

 

사용 방법은 변수나 함수 앞에 `global.`을 붙여주면 된다.

 

main.js

// 변수 선언 : global.변수명;
global.val = 1;

// 함수 선언 : gloabl.함수명 = function () { };
global.func = function () {
   console.log("I'm Global Function!!!");
}

 

renderer.js

var remote = require('electron').remote; 

// 변수 사용
console.log(remote.getGlobal('val'));   // 결과 : 1

// 함수 사용
remote.getGlobal("func")();   // 결과 : I'm Global Function!!!

 

 

 


[참고]  https://discuss.atom.io/t/how-to-set-global-variable-of-main-process/24833/3

반응형