将数组导出为ES6模块

Tob*_*oby 5 javascript ecmascript-6 es6-modules

由于某种原因,我无法使用导出为es6模块的数组:

export const choices = [
  ['first', 'First'],
  ['second', 'Second'],
  ['third', 'Third'],
]
Run Code Online (Sandbox Code Playgroud)

然后:

import { choices } from './constants'
console.log(choices) // undefined
Run Code Online (Sandbox Code Playgroud)

如果我只是const在尝试使用它的同一个文件中声明,则它会按预期工作。

T.J*_*der 6

浏览器上下文中的模块使用相对 URL,包括扩展名。所以导入应该是from './constants.js'而不仅仅是from './constants'. (不过,后者在 Node.js 上会很好,因为它具有当前的实验模块支持。)