简体   繁体   English

为什么我不能使用require()导入的javascript文件中的函数?

[英]Why can't I use functions from javascript-files that are imported by require()?

I started using electron . 我开始使用电子

In the index.html of electron-quick-start a JavaScript file is included using require() . 电子快速启动index.html中,使用require()包含JavaScript文件。

<script>
  // You can also require other files to run in this process
  require('./renderer.js')
</script>

Now I defined a simple function named flash() in renderer.js , as well as a log output: 现在我在renderer.js定义了一个名为flash()的简单函数,以及一个日志输出:

function flash(text) {
  alert("Text: " + text + "!");
}

console.log("Renderer loaded.");

After starting the electron app, I the the log-output in the console of the dev-tools. 启动电子应用程序后,我在dev-tools的控制台中输出日志。 but calling flash() does not work. 但调用flash()不起作用。

When including the script using 使用时包含脚本

<script src='./renderer.js'></script>

I can call the function. 我可以调用这个函数。

  • Where does the require() function come from? require()函数来自哪里?
  • Why can't I use the function when including the file using require ? 为什么我在使用require包含文件时不能使用该功能?
  • How can I use function defined in files that are required? 如何使用所需文件中定义的函数?
  • When should I use require() and when should I use src="" ? 我什么时候应该使用require() ,什么时候应该使用src=""

Where does the require() function come from? require()函数来自哪里?

The require in Electron is pretty much similar to require in Node.js. require在电子是非常相似, require在Node.js的 Electron is not just a web browser; Electron不仅仅是一个网络浏览器; it is intended for you to build desktop applications using HTML, CSS, and JavaScript. 它旨在使用HTML,CSS和JavaScript构建桌面应用程序。 Because it is intended for more than the web, I assume that the creators of Electron added their own little touch to make it an even more awesome piece of technology that you can use. 因为它的目的不仅仅是网络,我认为Electron的创造者添加了他们自己的小触摸,使其成为您可以使用的更加精彩的技术。 You can read more about it here: https://nodejs.org/api/modules.html#modules_modules . 您可以在此处阅读更多相关信息: https//nodejs.org/api/modules.html#modules_modules

Why can't I use the function when including the file using require? 为什么我在使用require包含文件时不能使用该功能?

It's because they are enclosed inside the module, and hence why it isn't available to any other script files. 这是因为它们被封装在模块内部,因此它不能用于任何其他脚本文件。

How can I use function defined in files that are required? 如何使用所需文件中定义的函数?

In order to use the flash function, you would need to export it, like so: 要使用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.");

But that alone won't let you readily use the flash function; 但仅凭这一点就不会让你随时使用flash功能; you will have to explicitly grab it from the require call like so: 你必须明确地从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(),什么时候应该使用src =“”?

Disclaimer: my opinion. 免责声明:我的意见。

Prefer to use require always. 总是require使用require Only use script src='' when you want to import libraries that don't use require but instead opt to declare variables globally. 如果要导入不使用require库,而是选择全局声明变量,则只使用script src=''

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM