【问题标题】:How should multiple HTML files be rendered in a single window within Electron?如何在 Electron 的单个窗口中呈现多个 HTML 文件?
【发布时间】:2019-08-23 01:54:55
【问题描述】:

我一直在使用 Electron,在尝试了几个模板、应用程序并浏览了 Electron 网站上的应用程序之后,我对如何在单个框架和 BrowserWindow 中呈现多个 HTML 文件感到有些困惑。

通过我了解的主题进行研究,我将使用BrowserWindowapp.on ready 创建一个新窗口,但我想建立一个侧导航,将内容加载到div,例如:

.navbar-global {
  background-color: grey;
}
.navbar-global .navbar-brand {
  color: white;
}
.navbar-global .navbar-user > li > a {
  color: white;
}
.navbar-primary {
  background-color: #333;
  bottom: 0px;
  left: 0px;
  position: absolute;
  top: 51px;
  width: 200px;
  z-index: 8;
  overflow: hidden;
  -webkit-transition: all 0.1s ease-in-out;
  -moz-transition: all 0.1s ease-in-out;
  transition: all 0.1s ease-in-out;
}
.navbar-primary.collapsed {
  width: 60px;
}
.navbar-primary.collapsed .glyphicon {
  font-size: 22px;
}
.navbar-primary.collapsed .nav-label {
  display: none;
}
.btn-expand-collapse {
    position: absolute;
    display: block;
    left: 0px;
    bottom:0;
    width: 100%;
    padding: 8px 0;
    border-top:solid 1px #666;
    color: grey;
    font-size: 20px;
    text-align: center;
}
.btn-expand-collapse:hover,
.btn-expand-collapse:focus {
    background-color: #222;
    color: white;
}
.btn-expand-collapse:active {
    background-color: #111;
}
.navbar-primary-menu,
.navbar-primary-menu li {
  margin:0; padding:0;
  list-style: none;
}
.navbar-primary-menu li a {
  display: block;
  padding: 10px 18px;
  text-align: left;
  border-bottom:solid 1px #444;
  color: #ccc;
}
.navbar-primary-menu li a:hover {
  background-color: #000;
  text-decoration: none;
  color: white;
}
.navbar-primary-menu li a .glyphicon {
  margin-right: 6px;
}
.navbar-primary-menu li a:hover .glyphicon {
  color: orchid;
}
.main-content {
  margin-top: 60px;
  margin-left: 200px;
  padding: 20px;
}
.collapsed + .main-content {
  margin-left: 60px;
}
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<nav class="navbar navbar-inverse navbar-global navbar-fixed-top">
      <div class="container-fluid">
        <div class="navbar-header">
          <a class="navbar-brand" href="#">Dash</a>
        </div>
        <div id="navbar" class="collapse navbar-collapse">
          <ul class="nav navbar-nav navbar-user navbar-right">
            <li><a href="#"><span class="glyphicon glyphicon-user"></span> User</a></li>
            <li><a href="#about"><span class="glyphicon glyphicon-log-out"></span> Logout</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>
<nav class="navbar-primary">
  <ul class="navbar-primary-menu">
    <li>
      <a href="dashboard.html"><span class="glyphicon glyphicon-list-alt"></span><span class="nav-label">Dashboard</span></a>
      <a href="profile.html"><span class="glyphicon glyphicon-envelope"></span><span class="nav-label">Profile</span></a>
      <a href="settings.html"><span class="glyphicon glyphicon-cog"></span><span class="nav-label">Settings</span></a>
      <a href="notification.html"><span class="glyphicon glyphicon-film"></span><span class="nav-label">Notification</span></a>
      <a href="monitor.html"><span class="glyphicon glyphicon-calendar"></span><span class="nav-label">Monitor</span></a>
    </li>
  </ul>
</nav>
<div class="main-content">
<h1>I am the main content</h1>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Autem sint assumenda quae aliquid voluptatibus quia, ea, ad natus magni repellat earum, culpa iure tempore. Enim dolor eaque minima voluptas ducimus?</p>
</div>

在侧边导航中会有命名的 HTML 文件,例如:

dashboard.html
profile.html
settings.html
notification.html
monitor.html

使用onclick,如果我没有在初始框架内创建一个新窗口但内容不同,应该如何将它们加载到&lt;div class="main-content"&gt;&lt;/div&gt;?那会在渲染器和/或主程序中完成吗?

当我研究答案时,我看到了&lt;webview&gt;,但我不确定是否应该将&lt;div class="main-content"&gt;&lt;/div&gt; 替换为&lt;webview class="main-content"&gt;&lt;/webview&gt;。我看到的其他搜索结果:

在 Electron 中,如何将 HTML 文件加载到单个框架中,该过程是否适用于正在加载的任何文件?

【问题讨论】:

  • 问题说明你为此付出了很多努力。但是,可以使代码示例更短,以使这个问题更容易理解。 CSS 可以完全省略,HTML 可以减少到几行。

标签: javascript html window electron


【解决方案1】:

鉴于您想将本地 HTML 文件加载到现有 HTML 页面中,有两种选择:

选项 1 - fs 模块

你说这太过分了,但我认为它是“ajax”请求的本地对应物。

<!DOCTYPE html>
<html>
<body>
    <button id="my-button">Click me</button>
    <div id="my-div">First content</div>
</body>
<script type="text/javascript">
    var fs = require('fs');

    document.getElementById('my-button').addEventListener('click', function() {
        fs.readFile('external_content.html', function (err, data) {
            document.getElementById('my-div').innerHTML = data.toString()
        })
    })
</script>
</html>

选项 2 - iframe

另一种选择是使用iframe,它允许在不使用 javascript 的情况下更改网站的一部分。

<!DOCTYPE html>
<html>
<body>
    <a href="external_content.html" target="my-iframe">Click me</a>

    <iframe name="my-iframe" src="first_content.html"></iframe>
</body>
</html>

【讨论】:

  • 另一个选项是网页浏览;它们与 iframe 基本相同,只是它们在安全隔离方面存在一些差异,主要是更严格。 (就像让 webview-content 在单独的进程中运行)请参阅:electron.atom.io/docs/api/webview-tag
猜你喜欢
  • 2021-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-01
  • 1970-01-01
  • 2015-09-02
  • 2013-08-02
  • 1970-01-01
相关资源
最近更新 更多