[英]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.exports将index.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.