电子 v10.1.1 给出未捕获的类型错误:无法读取未定义的属性“对话框”,但相同的代码可以在电子 v9.3.0 中使用

2024-02-22

I am trying to upload a file in an electron app which works perfectly for electron v9.3.0 but as soon as I use electron v10.1.1, it gives the following error Uncaught TypeError: Cannot read property 'dialog' of undefined at this line const dialog = electron.remote.dialog; see the screenshot below. enter image description here

main.js内容如下

const { app, BrowserWindow } = require('electron') 

function createWindow () { 
// Create the browser window. 
const win = new BrowserWindow({ 
    width: 800, 
    height: 600, 
    webPreferences: { 
    nodeIntegration: true
    } 
}) 

// Load the index.html of the app. 
win.loadFile('src/index.html') 

// Open the DevTools. 
win.webContents.openDevTools() 
} 

// This method will be called when Electron has finished 
// initialization and is ready to create browser windows. 
// Some APIs can only be used after this event occurs. 
// This method is equivalent to 'app.on('ready', function())' 
app.whenReady().then(createWindow) 

// Quit when all windows are closed. 
app.on('window-all-closed', () => { 
// On macOS it is common for applications and their menu bar 
// To stay active until the user quits explicitly with Cmd + Q 
if (process.platform !== 'darwin') { 
    app.quit() 
} 
}) 

app.on('activate', () => { 
// On macOS it's common to re-create a window in the 
// app when the dock icon is clicked and there are no 
// other windows open. 
if (BrowserWindow.getAllWindows().length === 0) { 
    createWindow() 
} 
}) 

// In this file, you can include the rest of your 
// app's specific main process code. You can also 
// put them in separate files and require them here. 

index.js的内容如下

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

// Importing dialog module using remote 
const dialog = electron.remote.dialog;

var uploadFile = document.getElementById('upload'); 

// Defining a Global file path Variable to store 
// user-selected file 
global.filepath = undefined; 

uploadFile.addEventListener('click', () => { 
// If the platform is 'win32' or 'Linux' 
    if (process.platform !== 'darwin') { 
        // Resolves to a Promise<Object> 
        dialog.showOpenDialog({ 
            title: 'Select the File to be uploaded', 
            defaultPath: path.join(__dirname, '../assets/'), 
            buttonLabel: 'Upload', 
            // Restricting the user to only Text Files. 
            filters: [ 
                { 
                    name: 'Text Files', 
                    extensions: ['txt', 'docx'] 
                }, ], 
            // Specifying the File Selector Property 
            properties: ['openFile'] 
        }).then(file => { 
            // Stating whether dialog operation was 
            // cancelled or not. 
            console.log(file.canceled); 
            if (!file.canceled) { 
            // Updating the GLOBAL filepath variable 
            // to user-selected file. 
            global.filepath = file.filePaths[0].toString(); 
            console.log(global.filepath); 
            } 
        }).catch(err => { 
            console.log(err) 
        }); 
    } 
    else { 
        // If the platform is 'darwin' (macOS) 
        dialog.showOpenDialog({ 
            title: 'Select the File to be uploaded', 
            defaultPath: path.join(__dirname, '../assets/'), 
            buttonLabel: 'Upload', 
            filters: [ 
                { 
                    name: 'Text Files', 
                    extensions: ['txt', 'docx'] 
                }, ], 
            // Specifying the File Selector and Directory 
            // Selector Property In macOS 
            properties: ['openFile', 'openDirectory'] 
        }).then(file => { 
            console.log(file.canceled); 
            if (!file.canceled) { 
            global.filepath = file.filePaths[0].toString(); 
            console.log(global.filepath); 
            } 
        }).catch(err => { 
            console.log(err) 
        }); 
    } 
}); 

index.html的内容如下

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>Hello World!</title> 
    <!-- https://electronjs.org/docs/tutorial 
                        /security#csp-meta-tag -->
    <meta http-equiv="Content-Security-Policy"
        content="script-src 'self' 'unsafe-inline';" /> 
</head> 
<body> 
    <h1>Hello World!</h1> We are using node 
    <script> 
        document.write(process.versions.node) 
    </script>, Chrome 
    <script> 
        document.write(process.versions.chrome) 
    </script>, and Electron 
    <script> 
        document.write(process.versions.electron) 
    </script>. 

    <h3>File Upload in Electron</h3> 
    <button id="upload">Upload File</button> 
    
    <!-- Adding Individual Renderer Process JS File -->
    <script src="index.js"></script> 
</body> 
</html> 

const win = new BrowserWindow({ 
    width: 800, 
    height: 600, 
    webPreferences: { 
         enableRemoteModule: true,
         nodeIntegration: true
    } 
}) 

为了访问remote渲染器进程上的模块。我们需要启用enableRemoteModule as true因为这是默认的false从较新的版本。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

电子 v10.1.1 给出未捕获的类型错误:无法读取未定义的属性“对话框”,但相同的代码可以在电子 v9.3.0 中使用 的相关文章

  • JavaScript“类”的规范术语[关闭]

    Closed 这个问题是基于意见的 help closed questions 目前不接受答案 这是一个在 JavaScript 中定义 类 创建它的实例并调用它的方法 函数之一的简单示例 Define a class like this
  • 附加组件生成器:ContentScript 并返回附加组件代码?

    我正在使用 Firefox Add on Builder 这是我到目前为止所拥有的 main js var widgets require widget var tabs require tabs var data require self
  • 在沙盒中的服务器上运行不受信任的 JavaScript 代码

    我似乎不知道如何设置节点沙箱 它可以安全地运行不受信任的代码 并允许用户通过api调用与程序交互 系统输入和输出 我正在尝试在浏览器中设置一个控制台 以便用户从服务器运行自己的代码 是否有任何节点包支持此功能 或者我是否需要编写自己的节点虚
  • 如何在后台加载图像?

    问题 我正在创建一个专辑 所以每次按 时间 下一个 按钮我正在加载新图像 我想要实现的是 只有在新图像从服务器完全下载后 我才想从旧图像切换到新图像 实际上我不想在加载时显示部分图像 有什么解决办法吗 PS 类似的问题 https stac
  • 如何使用鼠标单击选择多个项目?

    This is the default jQueryUI display as a Grid Layouts demo here http jqueryui com demos selectable display grid I can s
  • Angular JS - 如何验证数字输入中的位数

    我们想要做的是 有一个仅接受 0 24 的输入 对于时间输入应用程序 这些是用户应该能够输入到输入中的值 0 1 2 3 4 5 6 7 8 9 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15
  • 使用西里尔字母的正则表达式

    我有一个用于文本区域字段中字数统计的 jQuery 函数 此外 它排除所有用 三重括号 封闭的单词 它对于拉丁字符效果很好 但对于西里尔字母句子有问题 我认为错误部分与正则表达式有关 field val replace g match b
  • 自定义过滤器在 Angular Hybrid 应用程序中不起作用

    我正在尝试将 AngularJS 1 6 应用程序与 Angular 5 一起转换为混合应用程序 我定义了以下简单过滤器 function use strict var filterId colorPicker angular module
  • Internet Explorer 的数组indexOf 实现

    有很多关于如何将 indexOf 实现放入数组原型中以便它可以在 Internet Explorer 下工作的解决方案 但是我偶然发现了一个问题 到目前为止我所看到的任何地方似乎都没有解决这个问题 使用非常一致的MDC 的实施 https
  • 如果只有一个元素发生变化,为什么 AngularJs 会更新数组/哈希映射的所有元素?

    我有一个简单的哈希图和一个以文本形式显示状态的简单方法 但是当我仅更新 1 个用户状态时 所有这些状态都会更新 为所有用户调用函数 有没有一种方法可以只更新一个元素而不是全部 示例代码在这里 只需看看当您单击 更改状态 按钮时控制台中会发生
  • Node.JS Web 服务器中的安全性

    所以 我正在学习 Node JS 到目前为止我很喜欢它 我已经有几个项目在工作了 我想我可以在其中使用nodejs 不过 我担心安全问题 如果我使用 Node JS http 模块编写自定义 Web 服务器 我是否可能非常容易受到攻击 Ap
  • 如何切换整个页面的深色主题?

    我已经成功地在 html 和 Flask 中按下复选框时切换深色主题和浅色主题 但是我怎样才能让深色主题覆盖整个页面 而不仅仅是一些 div 元素呢 边距仍然是浅色主题 CSS代码如下 container display flow widt
  • 解密Javascript源代码[关闭]

    很难说出这里问的是什么 这个问题是含糊的 模糊的 不完整的 过于宽泛的或修辞性的 无法以目前的形式得到合理的回答 如需帮助澄清此问题以便重新打开 访问帮助中心 help reopen questions 我已经编写了一段 JavaScrip
  • 更改导航栏悬停时 div 的背景图像

    我正在开发一个项目 我对 Javascript 很陌生 所以我想知道是否有 Jquery 代码或只是一个关于如何使背景图像在导航菜单悬停时更改的过程 例如将鼠标悬停在链接一上会将 div 的背景图像更改为图像 1 将鼠标悬停在链接二上会将
  • 删除 CSS Transitionend 事件侦听器不起作用

    我在尝试删除 css Transitionend 事件侦听器时遇到问题 我可以添加监听器 e addEventListener transitionend function event transitionComplete event pr
  • 如何在 ES6 类中使用静态变量?

    我正在尝试在 es6 中使用静态变量 我想声明一个静态变量count in Animal类并增加它 但是 我无法通过声明静态变量static count 0 所以我尝试了另一种方法 class Animal constructor this
  • 如何检查 URL 末尾是否有特定字符串

    我需要根据 URL 末尾的内容让覆盖层向下滑动 如果 URL 末尾有 faq 覆盖层下降 如何在 jQuery JavaScript 中做到这一点 如果您的网址看起来像这样http yourdomain com faq 你可以这样做 var
  • JavaScript 不是 DOM 的一部分吗?

    为什么即使从 DOM 中删除用于创建脚本的代码 脚本仍然可以运行 我遇到了一种情况 我想阻止损坏的脚本运行 查看我的帖子 https stackoverflow com questions 2685581 is there a way to
  • 内联 YouTube 视频在 iOS 上的 cordova 应用程序中不起作用

    我用 cordova 开发了一个移动应用程序 我确实需要能够播放内联 YouTube 视频 我尝试了一段时间来解决它 我设置了属性playsinline to 1在 YouTube iframe API 中 I put
  • 谷歌地图通过骨干javascript返回div标签但不显示

    我已经开始使用地理定位 我可以获得坐标等 我想在地图中显示它 但是当我将地图返回到 div 时 什么也没有显示 现在我查看了 div 地图正在返回 但只是不可见 这是有问题的 div 请注意 这似乎只是一个小地图的链接 a style di

随机推荐