导入函数时未定义参数(Javascript / Nodejs)

pgx2nnw8  于 2023-02-18  发布在  Java
关注(0)|答案(1)|浏览(104)

我到处都找遍了,但显然,我找不到一个解决方案。我需要通过导入其他文件中定义的各种函数来创建一个模块化的javascript应用程序,但当我运行调试工具时,我得到了这个错误:

C:\Program Files\nodejs\node.exe .\app.js
Uncaught ReferenceError ReferenceError: arr1 is not defined
    at sum (c:\Users\Desktop\tes\sum.js:10:18)
    at <anonymous> (c:\Users\Desktop\test\app.js:9:1)
    at run (internal/modules/esm/module_job:193:25)
    --- async function ---
    at runMainESM (internal/modules/run_main:55:21)
    at executeUserEntryPoint (internal/modules/run_main:78:5)
    at <anonymous> (internal/main/run_main_module:17:47)
Process exited with code 1

这是我的准则

  • 应用程序.js*
import { add } from "./sum.js";
var arr1 = [1, 2, 3];
var arr2 = [1, 2, 3];
var tab1 = [1, 2, 3];
var tab2 = [1, 2, 3];

sum( arr1, arr2, tab1 , tab2);
  • 总和.js*
export function add(){
var arr3 = arr1.map((x, i) => x + arr2[i]);
console.log(arr3)
}
  • 包.json*
{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
yyyllmsg

yyyllmsg1#

在sum.js中没有定义函数参数,请在函数定义中添加函数参数,如下所示:

export function add(arr1, arr2, tab1, tab2){
  var arr3 = arr1.map((x, i) => x + arr2[i]);
  console.log(arr3)
}

同样在app.js中,通过导入函数的名称调用它

import { add } from "./sum.js";
var arr1 = [1, 2, 3];
var arr2 = [1, 2, 3];
var tab1 = [1, 2, 3];
var tab2 = [1, 2, 3];

add(arr1, arr2, tab1 ,tab2);

相关问题