为什么我不能使用由 require() 导入的 javascript 文件中的函数?
Why can't I use functions from javascript-files that are imported by require()?
我开始使用 electron。
在 index.html of electron-quick-start 中使用 require()
.
包含了一个 JavaScript 文件
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
现在我在renderer.js
中定义了一个名为flash()
的简单函数,以及一个日志输出:
function flash(text) {
alert("Text: " + text + "!");
}
console.log("Renderer loaded.");
启动电子应用程序后,我在开发工具的控制台中输出了日志。但是调用 flash()
不起作用。
包含脚本时使用
<script src='./renderer.js'></script>
我可以调用函数。
require()
函数从何而来?
- 为什么我在使用
require
包含文件时无法使用该功能?
- 如何使用所需文件中定义的函数?
- 什么时候应该使用
require()
,什么时候应该使用src=""
?
Where does the require() function come from?
Electron 中的 require
与 Node.js 中的 require
非常相似。 Electron 不仅仅是一个网络浏览器;它旨在让您使用 HTML、CSS 和 JavaScript 构建桌面应用程序。因为它的目的不仅仅是为了网络,我认为 Electron 的创造者添加了他们自己的一点点接触,使它成为您可以使用的更棒的技术。您可以在这里阅读更多相关信息:https://nodejs.org/api/modules.html#modules_modules.
Why can't I use the function when including the file using require?
这是因为它们 enclosed 在模块内部,因此它对任何其他脚本文件不可用。
How can I use function defined in files that are required?
为了使用 flash
函数,您需要将其导出,如下所示:
function flash(text) {
alert("Text: " + text + "!");
}
module.exports.flash = flash;
// Note: this is how we export. We assign properties to the `module.exports`
// property, or reassign `module.exports` it to something totally
// different. In the end of the day, calls to `require` returns exactly
// what `module.exports` is set to.
console.log("Renderer loaded.");
但仅此一项不会让您轻松使用 flash
功能;你不得不
像这样从 require
调用中明确获取它:
<script>
// You can also require other files to run in this process
var renderer = require('./renderer.js');
renderer.flash('Some text');
</script>
When should I use require() and when should I use src=""?
免责声明:我的意见。
更喜欢始终使用 require
。当您想要导入不使用 require
而是选择全局声明变量的库时,仅使用 script src=''
。
我开始使用 electron。
在 index.html of electron-quick-start 中使用 require()
.
<script>
// You can also require other files to run in this process
require('./renderer.js')
</script>
现在我在renderer.js
中定义了一个名为flash()
的简单函数,以及一个日志输出:
function flash(text) {
alert("Text: " + text + "!");
}
console.log("Renderer loaded.");
启动电子应用程序后,我在开发工具的控制台中输出了日志。但是调用 flash()
不起作用。
包含脚本时使用
<script src='./renderer.js'></script>
我可以调用函数。
require()
函数从何而来?- 为什么我在使用
require
包含文件时无法使用该功能? - 如何使用所需文件中定义的函数?
- 什么时候应该使用
require()
,什么时候应该使用src=""
?
Where does the require() function come from?
Electron 中的 require
与 Node.js 中的 require
非常相似。 Electron 不仅仅是一个网络浏览器;它旨在让您使用 HTML、CSS 和 JavaScript 构建桌面应用程序。因为它的目的不仅仅是为了网络,我认为 Electron 的创造者添加了他们自己的一点点接触,使它成为您可以使用的更棒的技术。您可以在这里阅读更多相关信息:https://nodejs.org/api/modules.html#modules_modules.
Why can't I use the function when including the file using require?
这是因为它们 enclosed 在模块内部,因此它对任何其他脚本文件不可用。
How can I use function defined in files that are required?
为了使用 flash
函数,您需要将其导出,如下所示:
function flash(text) {
alert("Text: " + text + "!");
}
module.exports.flash = flash;
// Note: this is how we export. We assign properties to the `module.exports`
// property, or reassign `module.exports` it to something totally
// different. In the end of the day, calls to `require` returns exactly
// what `module.exports` is set to.
console.log("Renderer loaded.");
但仅此一项不会让您轻松使用 flash
功能;你不得不
像这样从 require
调用中明确获取它:
<script>
// You can also require other files to run in this process
var renderer = require('./renderer.js');
renderer.flash('Some text');
</script>
When should I use require() and when should I use src=""?
免责声明:我的意见。
更喜欢始终使用 require
。当您想要导入不使用 require
而是选择全局声明变量的库时,仅使用 script src=''
。