티스토리 뷰

electron에서 ipcRenderer 사용하기 - 제2편 ipcRenderer import하기

 

 

 

루트(/)에 있는 /index.js를 잘라내기 해서 현재 비어있는 /src/main/으로 덮어쓰기 한다.

 

안에 내용을 복사해다가 붙혀도 된다. 루트에 있는 index.js를 없애면 된다.

 

 

 

/src/main/index.js

const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

const path = require('path');
const url = require('url');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

function createWindow () {
  mainWindow = new BrowserWindow();

  mainWindow.loadURL(`file://$/../../index.html`);

  // Open the DevTools.
  // mainWindow.webContents.openDevTools()

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
});

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow()
  }
});
 

복붙을 한 후에 위에처럼 17번 라인에 

 

mainWindow.loadURL(`file://$/../../index.html`);

이 부분을 바꿔준다.

 

 

그리고 package.json의 main

 

1
"main""index.js",
cs

 

 

1
"main""./dist/main/index.js",
cs

dist/main 아래에 있는 index.js로 바꿔준다.

 

 

package.json

{
  "name": "electron_practice_02",
  "version": "1.0.0",
  "description": "",
  "main": "./dist/main/index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "electron .",
    "build": "webpack",
    "watch": "webpack -w",
    "electron": "electron ."
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "electron": "^1.7.8"
  }
}​

 

 

 

app.js

import  from 'electron';

ipcRenderer.on('PRINT_TEXT', (_e, text) => {
  try {
    alert(text);
    console.log('text:', text);
  } catch (e) {
    console.log(e);
  }
});

ipcRenderer.send('REQUEST_EVENT', 'request_event');
 
index.js
import { ipcMain } from 'electron';

const electron = require('electron');
// Module to control application life.
const app = electron.app;
// Module to create native browser window.
const BrowserWindow = electron.BrowserWindow;

const path = require('path');
const url = require('url');

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;

function createWindow () {
  mainWindow = new BrowserWindow();
  mainWindow.loadURL(`file://$/../../index.html`);

  ipcMain.on('REQUEST_EVENT', (_e, eventName) => {
    mainWindow.webContents.send('PRINT_TEXT', 'send_text');
    console.log(eventName);
  });

  // Emitted when the window is closed.
  mainWindow.on('closed', function () {
    mainWindow = null
  })
}

app.on('ready', createWindow);

// Quit when all windows are closed.
app.on('window-all-closed', function () {
  if (process.platform !== 'darwin') {
    app.quit()
  }
});

app.on('activate', function () {
  if (mainWindow === null) {
    createWindow();
  }
});

 

end.

 

 

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함