admin管理员组文章数量:1323529
How do I call a function which is stored in an other function?
I created two js files (File A and File B). In file A I created a function called "getFiles" and I would like to call this function in File B.
File A:
export function getFiles(input){
...
}
File B:
import {getFiles} = from './A.js';
getFiles('');
But I'm getting the error message "Module '"./A.js"' has no exported member 'getFiles'."
Does anyone know how I can call the function in File B?
How do I call a function which is stored in an other function?
I created two js files (File A and File B). In file A I created a function called "getFiles" and I would like to call this function in File B.
File A:
export function getFiles(input){
...
}
File B:
import {getFiles} = from './A.js';
getFiles('');
But I'm getting the error message "Module '"./A.js"' has no exported member 'getFiles'."
Does anyone know how I can call the function in File B?
Share Improve this question edited Feb 27, 2021 at 22:12 Dominic Saladin asked Jun 22, 2020 at 4:50 Dominic SaladinDominic Saladin 1611 gold badge2 silver badges11 bronze badges 4- Provide code of what you have tried? – Sean Commented Jun 22, 2020 at 5:46
-
which web pages have you read about
export
in JavaScript? – rioV8 Commented Jun 22, 2020 at 6:04 - why do you post images of code, do you expect us to type it all in, and this is not searchable by SO or Google – rioV8 Commented Jun 22, 2020 at 6:41
-
why don't you load/import the file the same as you do for
vscode
? – rioV8 Commented Jun 22, 2020 at 6:42
2 Answers
Reset to default 3You can use import/export.
For example:
file A.js:
export function func1(){
...
}
file B.js:
import { func1 } from './A.js';
...
func1();
In the js File A I had to create a class to be able to export the function. The class had to be exported with "module.exports"
module.exports = class A {
getFiles(input) {
...
}
}
And now in File B I had to add a require line to this class:
const A = require('./A.js');
And now I can call my function "getFiles" from File A:
// vscode extension function
function activate(context) {
const a = new A();
a.getFiles('');
}
https://nodejs/api/modules.html#modules_modules
本文标签: javascriptHow do I call a JS Function in another fileStack Overflow
版权声明:本文标题:javascript - How do I call a JS Function in another file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742129697a2422103.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论