使用Babel,我无法导入以下模块:
// mongoose_helpers.js
const r_string = {
type: String,
required: true
}
const r_number = {
type: Number,
required: true
}
export default { r_string, r_number }
这样:
import { r_string, r_number } from './mongoose_helpers'
这会在我导入的文件中引发错误。
但是,如果我这样做,它会起作用:
const r_string = {
type: String,
required: true
}
(...)
const helpers = { r_string, r_number }
export default helpers
然后
import helpers from './mongoose_helpers'
只是好奇第一种方法有什么问题?
答案 0 :(得分:4)
export default { r_string, r_number }
基本上是用两个键(r_string,r_number)
它适用于import helpers from './mongoose_helpers'
当您说import { r_string, r_number } from './mongoose_helpers'
时,您告诉JS您希望模块中有两个命名导出。这不是解构语法。要使用它,您需要指定像这样的导出
export const r_string = ...
export const r_number = ...
关于ES6模块的最终文章:http://www.2ality.com/2014/09/es6-modules-final.html