电子-如何将html文件加载到当前窗口? [英] Electron - How to load a html file into the current window?
问题描述
我一直在寻找有关文档,谷歌等的信息,它们是如何在电子应用程序的主窗口中加载html文件的,但是我找不到方法。
I was looking all around: docs, google, etc., on how to load a html file in the main window of an electron app, but I can't find a way.
这真的很复杂吗?
我想到的是ajax,因此有效:
With what I have came up is ajax, thus works:
$("#main").load("./views/details.html");
我发现的另一种方法是通过遥控器:
Another method I have found is via remote:
const {BrowserWindow} = require('electron').remote
let win = new BrowserWindow({width: 800, height: 600})
win.loadURL('https://github.com')
但这总是打开一个新窗口,并且我需要替换现有页面
But this opens always a new window, and I need to replace the existing page
推荐答案
如果要在现有窗口中加载新的URL,可以在渲染器过程:
If you want to load a new URL in an existing window you can do this in the renderer process:
const { remote } = require('electron')
remote.getCurrentWindow().loadURL('https://github.com')
请注意,当新的URL已加载,因此发生这种情况时您可能会看到闪烁。这就是为什么在构建Electron应用程序时通常最好使用单页应用程序(SPA)架构。
Note that Electron restarts the renderer process when a new URL is loaded, so you'll probably see a flash when that happens. This is why it's usually best to use a single page application (SPA) architecture when building Electron apps.
这篇关于电子-如何将html文件加载到当前窗口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!