Javascript 通过index.js公开时ES6导入不工作
我有一个奇怪的问题。我有一个索引文件,如:Javascript 通过index.js公开时ES6导入不工作,javascript,Javascript,我有一个奇怪的问题。我有一个索引文件,如: // index.js import * as constants from './constants'; export { constants, ... }; // constants.js const CUSTOMER_CREATE_SUCCESS_KAFKA_PAYLOAD = '...'; const CUSTOMER_CREATE_ERROR_KAFKA_PAYLOAD = '...'; const ACCOUNT_CREATE_
// index.js
import * as constants from './constants';
export {
constants,
...
};
// constants.js
const CUSTOMER_CREATE_SUCCESS_KAFKA_PAYLOAD = '...';
const CUSTOMER_CREATE_ERROR_KAFKA_PAYLOAD = '...';
const ACCOUNT_CREATE_SUCCESS_KAFKA_PAYLOAD = '...';
const ACCOUNT_CREATE_ERROR_KAFKA_PAYLOAD = '...';
const ValidationSchemas = {
CUSTOMER_CREATE_SUCCESS_KAFKA_PAYLOAD,
CUSTOMER_CREATE_ERROR_KAFKA_PAYLOAD,
ACCOUNT_CREATE_SUCCESS_KAFKA_PAYLOAD,
ACCOUNT_CREATE_ERROR_KAFKA_PAYLOAD,
};
export {
ValidationSchemas,
}
我可以直接访问常量文件
// consumers/handler.js
import * as constants from '../constants';
const { ValidationSchemas } = constants;
但当我这么做的时候
import { constants } from '..'; // trying to access via index.js
const { ValidationSchemas } = constants;
我明白了
TypeError:无法对“\u0.constants”的属性“ValidationSchemas”进行解构,因为它未定义。
反对。(/data/…)
at模块编译(内部/modules/cjs/loader.js:1156:30)
at模块编译(/data/…)
at Module._extensions..js(internal/modules/cjs/loader.js:1176:10)
有什么问题?@Sirko
从…导入{constants}
不会进行解构。并且export{constants}
是否在index.js中创建了名为constants
的导出。是否使用transpiler?你有循环依赖关系吗?你能举一个简单的例子来说明复制这个文件的必要性吗?您确定导入的文件正确吗?是的,使用了babel。让我试试看example@Bergi发现问题。。。你是对的,我是dependency@Sirkoimport{constants}from…
不执行解构。并且export{constants}
是否在index.js中创建了名为constants
的导出。是否使用transpiler?你有循环依赖关系吗?你能举一个简单的例子来说明复制这个文件的必要性吗?您确定导入的文件正确吗?是的,使用了babel。让我试试看example@Bergi发现问题。。。你说得对,循环依赖
TypeError: Cannot destructure property 'ValidationSchemas' of '_.constants' as it is undefined.
at Object.<anonymous> (/data/...)
at Module._compile (internal/modules/cjs/loader.js:1156:30)
at Module._compile (/data/...)
at Module._extensions..js (internal/modules/cjs/loader.js:1176:10)