由于某些原因,我无法使用作为ES6模块导出的数组:
export const choices = [
['first', 'First'],
['second', 'Second'],
['third', 'Third'],
]
那么:
import { choices } from './constants'
console.log(choices) // undefined
如果我在尝试使用它的同一文件中简单地声明const
,它会按预期工作。
由于某些原因,我无法使用作为ES6模块导出的数组:
export const choices = [
['first', 'First'],
['second', 'Second'],
['third', 'Third'],
]
那么:
import { choices } from './constants'
console.log(choices) // undefined
如果我在尝试使用它的同一文件中简单地声明const
,它会按预期工作。
浏览器环境中的模块使用相对URL,包括扩展名。因此,导入应该是from './constants.js'
而不仅仅是from './constants'
。(后者在Node.js上是可以的,但它当前处于实验性模块支持状态。)
.js
可以解决这个问题。我正在使用 Facebook 的 create-react-app,通过它我可以导入组件、类、函数和对象,无需指定文件扩展名。 - Tobyconstants
更改为helpers
,它就可以工作了。constants
是JavaScript或ES6中的受保护单词吗?我知道const
是,也许有关.. - Tobyconstants
。也许是你的加载器配置有问题,或者你有其他的constants.x
文件在你的Web服务器配置中具有优先级。在这种情况下,我期望在Web控制台中看到错误消息而不是记录undefined
。 - T.J. Crowder