[英]How can I import another JS file (which is in the same directory) in a electron JS file
我想使用const new = require("new.js")
在“main.js”中導入“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 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.