javascript - js文件中的导入导出方法不起作用
问题描述
我想在我的 js 文件中导入一个函数。这是一个简单的代码:
// In index.js file
function addition(a, b) {
return a + b;
}
export { addition };
// In test.js file
import { addition } from "./index.js";
console.log(addition(5, 4));
谢谢 !
解决方案
NodeJS 使用不支持的 CommonJS 模块语法import/export
,但需要module.exports
像这样使用:
// In index.js file
function addition(a, b) {
return a + b;
}
module.exports = addition;
// In test.js file
const addition = require("./index.js");
console.log(addition(5, 4));
推荐阅读
- vb.net - “是否有在鼠标指针选择时查找 DataGridView 中列值总和的功能?”
- c# - 如何从右向左移动多标签(一个和另一个)?
- java - 使用 JMX 参数运行 jar 时的端口绑定错误
- android - 如何将字符串数组的元素动态插入到 textview 中并定位它?
- php - 开发 laravel 5.8 包时找不到 Illuminate\Console\Command
- r - 如何通过另一个变量创建单独的表
- c++ - 非静态成员函数的 std::add_pointer 实现
- django - 了解 Django CSRF_COOKIE_SAMESITE 和 CSRF_TRUSTED_ORIGINS
- angular - 上传大于 70kb 的文件时连接重置,以 Angular 作为前端,Flask 作为后端
- python - Python中的分区时间戳格式转换