简体   繁体   English

如何在不运行整个js文件的情况下从文件导入javascript function?

[英]How to import javascript function from file without running entire js file?

I have page1.html which uses blah.js.我有使用 blah.js 的 page1.html。 One of the things blah.js does is run a function1() when the page is loaded. blah.js 做的一件事是在加载页面时运行 function1()。 I would like to use function2() from blah.js in page2.html without blah.js running function1().我想在 page2.html 中使用来自 blah.js 的 function2() 而没有 blah.js 运行 function1()。 I thought I could add我想我可以添加

export {function2}

to blah.js and then just import function2() by adding到 blah.js,然后通过添加导入 function2()

<script type = "module">
import { function2 } from './blah.js'
</script>

to page2.html.到 page2.html。 But this seems to have the same functionality as if I just imported the file as normal:但这似乎具有与我正常导入文件相同的功能:

  <script defer src="blah.js"></script>

ie function1() runs in page2 in both cases.即 function1() 在这两种情况下都在 page2 中运行。 How can I get access to function2() without running the other parts of blah.js?如何在不运行 blah.js 的其他部分的情况下访问 function2()?

If you import from a module, all of that module's top-level code will run.如果您从一个模块导入,该模块的所有顶级代码都将运行。 That is, given也就是说,给定

console.log('running');
export const foo = 5;

There is no way to import foo without also logging the text.如果不记录文本,则无法导入foo

One good approach to this problem (and a very good practice in general) is to avoid side-effects at the top level of modules.解决这个问题的一个好方法(通常也是一个非常好的做法)是避免模块顶层的副作用 If you have something that's a side-effect, put it into a function instead, and export that function, so that it only runs on demand, instead of every time anything from the file is imported.如果您有一些副作用,请将其放入 function,然后导出该 function,以便它仅按需运行,而不是每次导入文件中的任何内容时运行。

So, you probably want to change your file to something like:因此,您可能希望将文件更改为:

export const runFunction1WhenPageLoads = () => {
  const fnToRun = () => {
    // insert code here
  };
  if (document.readyState === 'complete') {
    fnToRun();
  } else {
    window.addEventListener('DOMContentLoaded', fnToRun);
  }
};
export const function2 = () => {
  // insert code here
};

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

相关问题 仅将函数(不是整个文件堆栈)从 vanilla JavaScript 导入 Angular - Import function only (not entire file stack) into Angular from vanilla JavaScript 是否可以在不将导出和导入添加到每个函数等的情况下导出和导入整个JS文件? - Is it possible to export and import an entire JS file without adding Export and Import to every function, etc? 从js文件运行javascript function - Running javascript function from a js file 如何使用 Webpack 包含/导入整个 JS 文件 - How to include/import an entire JS file with Webpack 如何从 util.js 文件导入 getToken 函数而不会出错? - How do I import getToken function from util.js file without getting an error? 如何将整个JavaScript文件包装在命名函数中? - How to wrap an entire JavaScript file in a named function? React.js / CRA,如何从另一个文件调用或导入javaScript函数? - React.js / CRA, How to call or import a javaScript function from another file? 如何从同一目录中的 JavaScript 文件导入 JavaScript 函数? - How to import a JavaScript function from a JavaScript file in the same directory? 如何使用 webpack 导入整个 js 文件? - How i can import an entire js file with webpack? 如何将 .js 文件中的 javascript 变量导入 html 文档 - How to import javascript variable from .js file to html document
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM