首页 文章

不允许加载本地资源 - 电子

提问于
浏览
4

当我通过Visual Studio Code运行我的Electron应用程序时,主进程会加载,进而启动index.html页面 . 在index.js脚本中,我将浏览器窗口重定向到名为startup.html的本地html文件,该文件位于我的脚本文件夹中,该文件夹只是应用程序的子文件夹 . index.html页面甚至没有启动,应用程序会生成错误消息:

Not allowed to load local resource

在DevTools控制台中,它还显示了它尝试加载的资源:

file:///usr/local/lib/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/scripts/ui/startup/startup.html

如果我从项目的根文件夹运行“npm start”,则应用程序会正确启动,并且会加载index.html和startup.html页面 .

Visual Studio Code启动电子:

/usr/local/bin/electron

这似乎与仅使用“npm start”启动它不同 . 不确定有什么区别 .

旁注:在我添加代码以启动startup.html之前,应用程序将从Visual Studio Code运行 . 只有在添加startup.html之后它才能正常工作 .

可能是什么导致了这个?

2 回答

  • 0

    显然,在更新版本的Electron中出现了一些与VS Code配置设置有关的变化 . 有关如何配置VS代码的文档已在以下位置更新:

    https://electronjs.org/docs/tutorial/debugging-main-process-vscode

  • 0

    因此,如果我们试图在不完全加载页面的情况下操纵页面内容,则可能会出现此问题 . 因此,任何操作都必须在正常加载页面后(在准备好显示事件之后)完成 .

    我也遇到了同样的问题,我在加载文件之前放置了以下行 .

    window.webContents.openDevTools()
    

    示例代码

    // Issue code
    window =  new BrowserWindow({width:800,height:600,parent:mainWindow})
    window.webContents.openDevTools()
    window.loadURL(url.format({
        pathname: path.join(__dirname,'/../views/file.html'),
        protocol: 'file',
        slashes: true
    }))
    
    // Issue Solved code 
    window =  new BrowserWindow({width:800,height:600,parent:mainWindow})
    
    window.loadURL(url.format({
        pathname: path.join(__dirname,'/../views/file.html'),
        protocol: 'file',
        slashes: true
    }))
    window.webContents.openDevTools()
    

相关问题