Electron - 如何将 html 文件加载到当前窗口中? [英] Electron - How to load a html file into the current window?

查看:34
本文介绍了Electron - 如何将 html 文件加载到当前窗口中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我四处寻找:文档、谷歌等,关于如何在电子应用程序的主窗口中加载 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.

真的这么复杂还是非常简单?

Is it really this complicated or dead simple?

我想出的是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 会重新启动渲染器进程,因此您可能会在这种情况下看到闪烁.这就是为什么在构建 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.

这篇关于Electron - 如何将 html 文件加载到当前窗口中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆