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
Add a ment  | 

2 Answers 2

Reset to default 3

You 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