简体   繁体   English

如何导入函数javascript

[英]How to import functions javascript

i need to export my functions from index.js我需要从 index.js 导出我的函数

And use it in app.js like myFunction() Dont var.myFunction() And whithout using并在 app.js 中使用它,如 myFunction() 不要 var.myFunction() 并且没有使用

{myFunction} = require("index.js")

Because i need import them all因为我需要全部导入

Assign all functions in index.js to module.exportsindex.js所有函数分配给 module.exports

for example , below content in your index.js例如,在你的 index.js 内容下面

module.exports= { t: x=> console.log ('this is '+x), add: (x,y) => x+y}

to import them use导入它们使用

const myFunctions =require('index.js')

and then you can call myFunctions.add(2,3)然后你可以调用myFunctions.add(2,3)

To learn more check module.exports in : https://learnjsx.com/category/2/posts/es6-moduleExports要了解更多信息,请检查 module.exports: https ://learnjsx.com/category/2/posts/es6-moduleExports

Please learn more about the CommonJS module specification.请了解有关 CommonJS 模块规范的更多信息。

https://flaviocopes.com/commonjs/ https://flaviocopes.com/commonjs/

Let's assume we have util.js .假设我们有util.js

exports.one = () => {}
exports.two = () => {}
exports.three = () => {}

Then as I know we can import everything like this.然后据我所知,我们可以像这样导入所有内容。

const Util = require('utils.js');

Util.one()
Util.two()
Util.three()

Using ES6 syntax使用 ES6 语法

import * as Util from 'utils.js'

Hope that answers your question.希望这能回答你的问题。

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

相关问题 如何正确导入函数(Javascript) - How to import functions correctly (Javascript) 如何在List函数中导入Javascript库 - How to import a Javascript library in a List Functions 如何在jsx中导入和使用javascript函数? - How to import&use javascript functions in jsx? 在javascript中隐式导入函数 - Import functions implicitly in javascript 在 JavaScript 中,如何像在 Python 中一样简单地导入类和函数? - In JavaScript, how can one import classes and functions simply, like in python? 如何从其他javascript文件导入函数? - How can I import functions from an other javascript file? 如何在 JavaScript 中的 import() 调用后使导出函数全局化 - How to make the export functions global after an import() call in JavaScript 如何导入或使用javascript中第三方命名空间中定义的函数? - How to import or use functions defined in third party namespace in javascript? 如何将模块中的多个函数导入 JavaScript 中的一个对象? - How to import multiple functions from module into one object in JavaScript? 如何以与Node.js和浏览器JavaScript同时兼容的方式跨文件导入JavaScript函数 - How to import JavaScript functions across files in manner that is simultaneously compatible with Node.js and with browser JavaScript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM