Javascript 无法使用electronjs和jquery显示OpenDialog

Javascript 无法使用electronjs和jquery显示OpenDialog,javascript,jquery,electron,Javascript,Jquery,Electron,我正在尝试使用electronjs创建一个简单的桌面应用程序。我的目标是打开一个ShowOpenDialog,但是(我不知道为什么)它什么也不打开。 树状视图: sample app |-index.html |-js |--jquery.js |--index.js |-main.js |-package.json index.html <!DOCTYPE html> <html> <head> <meta chars

我正在尝试使用electronjs创建一个简单的桌面应用程序。我的目标是打开一个
ShowOpenDialog
,但是(我不知道为什么)它什么也不打开。
树状视图:

sample app
 |-index.html
 |-js
   |--jquery.js
   |--index.js
 |-main.js
 |-package.json
index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Hello World!</title>
  </head>
  <body>
    <h1>Hello World!</h1>
    <br />
    <button id="openFile">Open</button>
    <script>
        window.$ = window.jQuery = require('./js/jquery.js');
    </script>
    <sctipt src ="./js/index.js"></sctipt>
</html>
main.js

'use strict';
const electron = require('electron');
const dialog = require('electron').dialog;
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;

let mainWindow;

function createWindow () {
  mainWindow = new BrowserWindow({width: 800, height: 600});
  mainWindow.loadURL('file://' + __dirname + '/index.html');
  mainWindow.
  mainWindow.on('closed', function() {
    mainWindow = null;
  });
}

app.on('ready', createWindow);

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

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

{
  "name": "electron-quick-start",
  "version": "1.0.0",
  "description": "A minimal Electron application",
  "main": "main.js",
  "scripts": {
    "start": "electron main.js"
  }
}

您需要在主进程中使用
对话框
,但请尝试在渲染器进程中使用它。这不行。您应该使用
remote
模块获取对主进程
对话框的引用,或者使用
ipc
模块向主进程发送消息以打开对话框


举个简单的例子,尝试用
require('electron').remote.require('dialog')
替换index.js中的
dialog
;但是,从长远来看,我建议改用IPC。

您不能将渲染器进程作为主进程

您可以像这样使用remote或ipc

index.js

$(document).ready(function() {    
    $("#openFile").click(function(){
        dialog.showOpenDialog(function (fileNames) {
        }); 
    })    
})
const electron = require("electron")
const ipc = electron.ipcRenderer

$(document).ready(function() {    
    $("#openFile").click(function(){
        ipc.send("openF")
    })    
})
在main.js中

哦,是的,因为你需要文件路径,你需要在index.js上使用它

ipcRenderer.on("filepaths", function(event, message) {
  if(message == undefined) return
  else{
    console.log(message); // Logs filepaths

    if(fl !== ""){
     console.log("no filepath found")
    }
}
// if you wanna open file use fs
})
ipcRenderer.on("filepaths", function(event, message) {
  if(message == undefined) return
  else{
    console.log(message); // Logs filepaths

    if(fl !== ""){
     console.log("no filepath found")
    }
}
// if you wanna open file use fs
})