我正在尝试编写一个程序,使用openweathermap.org API显示天气数据,同时使用MaterialiseCSS使其看起来漂亮漂亮。但是,目前我的代码似乎没有在Electron中加载与在我的Web浏览器中相同的代码。它在Web浏览器(最新版本的Google Chrome)中正常工作,而在Electron中,按钮和动画根本无法正常工作。没有动画,并且不同的选项卡无法正确显示。单击时,选项卡不执行任何操作,并且一次显示选项卡的所有内容。见下文。
https://i.stack.imgur.com/PdR4z.png
文件正确显示在浏览器中,如下所示。
https://i.stack.imgur.com/kAYpS.png
以下是index.html的代码:
const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')
const functions = require('./functions')
let win;
function createWindow () {
win = new BrowserWindow({width: 1280, height: 720});
win.loadURL('file://' + __dirname + '/index.html');
functions.getData(function(data) {
console.log(data);
});
//win.webContents.openDevTools()
win.on('closed', () => {
win = null
});
}
app.on('ready', createWindow);
app.on('window-all-closed', () => {
// mac stuff
if (process.platform !== 'darwin') {
app.quit()
}
});
app.on('activate', () => {
// more mac stuff
if (win === null) {
createWindow()
}
});
// materializecss.com for the the css used.
<!DOCTYPE html>
<html>
<head>
<!-- micron.js -->
<link href="https://unpkg.com/webkul-micron@1.0.4/dist/css/micron.min.css" type="text/css" rel="stylesheet">
<!-- -->
<!-- materialize -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<link rel="stylesheet" href="/css/style.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<!-- -->
<!-- scripts -->
<script type="text/javascript" src="./functions.js"></script>
<!-- -->
<meta charset="utf-8">
<title>Material Weather</title>
</head>
<body>
<nav class="nav-extended light-blue">
<div class="nav-wrapper light-blue">
<a href="#" class="brand-logo center light-blue">Material Weather</a>
<a href="#" data-activates="mobile-demo" class="button-collapse"><i class="material-icons">menu</i></a>
</div>
<div class="nav-content light-blue">
<ul class="tabs tabs-fixed-width light-blue">
<li class="tab"><a href="#today" class="white-text">Today</a></li>
<li class="tab"><a href="#tomorrow" class="white-text">Tomorrow</a></li>
<li class="tab"><a href="#10day" class="white-text">10 Day</a></li>
</ul>
</div>
</nav>
<div id="today" class="col s12">Today</div>
<div id="tomorrow" class="col s12">Tomorrow</div>
<div id="10day" class="col s12">10 Day</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script src="https://unpkg.com/webkul-micron@1.0.4/dist/script/micron.min.js" type="text/javascript"></script>
</body>
</html>
如果您需要任何其他信息,请随时提出。提前感谢您的时间。
答案 0 :(得分:2)
问题是jQuery未在Electron窗口中正确加载。经过几个小时的互联网搜索试图找到解决办法后,这就是我想出来的。
将其放在代码的脚本部分的开头:
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script>
将它放在脚本部分的末尾:
<script>if (window.module) module = window.module;</script>
From what I've read,它实质上强制窗口使用导入的每个模块,这是其他类似问题的常见修复。
所以,我的代码现在看起来像这样:
<script>if (typeof module === 'object') {window.module = module; module = undefined;}</script> <!-- makes jquery actually work -->
<!-- scripts -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<script src="https://unpkg.com/webkul-micron@1.0.4/dist/script/micron.min.js" type="text/javascript"></script>
<!-- -->
<script>if (window.module) module = window.module;</script> <!-- makes jquery actually work -->```
答案 1 :(得分:0)
这是一个疯狂的猜测,因为我只使用Electron构建了一个应用程序,但我确实有类似的问题,我必须在package.json文件中包含dependancies。
"dependencies": {
"bootstrap": "^3.3.7"
},