javascript - 如何在电子 JS 文件中导入另一个 JS 文件(位于同一目录中)
问题描述
我想在“main.js”中导入“new.js”,使用 const new = require("new.js")
,我也试过import "./new.js"
,但我不想工作。
这是我的代码,如果你想看的话
// Modules to control application life and create native browser window
const { app, BrowserWindow } = require('electron')
const path = require('path')
function createWindow() {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true
}
})
// and load the index.html of the app.
mainWindow.loadFile('index.htm')
// Open the DevTools.
// mainWindow.webContents.openDevTools()
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
createWindow()
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
// Quit when all windows are closed.
app.on('window-all-closed', function () {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') app.quit()
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
//password
我希望你能帮助我,请不要评判我的编码技能,我还在学习。无论如何,这里也有我的 html 和 css 代码。
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<meta http-equiv="X-Content-Security-Policy" content="default-src 'self'; script-src 'self'">
<link rel="stylesheet" href="index.css">
<link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@0,400;0,700;1,400&display=swap" rel="stylesheet">
<title>FYco</title>
</head>
<body>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<h1>FYco app</h1>
<form id="whoAreyou">
<label for="whoAreyou">
Chi sei?
</label>
<br>
<select>
<option value="1">Federico Santucci</option>
<option value="2">Yuri de Marco</option>
<option value="3">Simone Stroppiana</option>
<option value="4">Edoardo Casali</option>
<option value="5">Guido Betti</option>
</select>
<br>
<label for="password">Password</label>
<br>
<div id="password">
<input id="password" type="password">
</div>
<form id="send">
<input id="send" type="submit">
</form>
</form>
<!-- You can also require other files to run in this process -->
<script src="./renderer.js"></script>
<script src="new.js"></script>
</body>
</html>
html, body {
margin: 0;
padding: 0;
background-color: #333;
}
select {
text-align: center;
border: 3px solid #555;
width: 25em; height: 3em;
font-family: 'Lato', sans-serif;
font-style: italic;
}
h1 {
color: #f1f1f1;
font-size: 48px;
font-family: 'Lato', sans-serif;
text-align: center;
}
label {
text-align: center;
color: #f1f1f1;
font-size: 48px;
font-family: 'Lato', sans-serif;
font-style: italic;
}
input[type=password] {
border: 3px solid #555;
width: 25em; height: 3em;
}
input[type=submit] {
border: 3px solid #555;
width: 25em; height: 3em;
}
/* IDs */
#password {text-align: center}
#whoAreyou{text-align: center}
/*
font-family: 'Lato', sans-serif;
*/
我希望我的所有代码对你来说都很清楚。
解决方案
我们需要先查看new.js
文件的内容以及如何导出模块。
确保您正在导出您需要使用的内容module.exports
例如
新的.js
const newObj = { a: 5, b:6 };
module.exports = newObj;
main.js
const importedObj = require('./new.js');
console.log(importedObj);
// will output { a: 5, b:6 }
推荐阅读
- c++ - 如何忽略文件底部的空格?
- javascript - 如何从谷歌工作表脚本在浏览器选项卡中打开网站?
- java - 你能解释一下扫描仪的工作原理吗?它打印所有内容吗?
- sulu - Sulu CMS:如何在管理视图中显示模板提示
- php - 离开文件夹或某些 url 时结束会话
- php - 如何在php中打印多维数组值
- tensorflow - Determining max batch size with TensorFlow Object Detection API
- mysql - 在mysql中显示2次数据
- python-3.x - 要从数据框的一列中获取条目,请将其属性隔离并根据该属性将它们放置到不同的列中
- html - 如何在中心和左对齐背景覆盖图像上制作带有垂直文本的全高图像