export default { foo: 'bar' }// CommonJS module.exports = { foo: 'bar' }
export var foo = 'bar' // CommonJS module.exports.foo = 'bar'
export var baz = 'ponyfoo'
import _ from 'lodash'// var _ = require('lodash')
import {map, reduce} from 'lodash'
import {map as myMay} from 'lodash'
import * as _ from 'lodash'
导入所有必须加 as
语句。
demo.js 如下
export default {
a:1
}
export var other = {
b:2,
c:3
}
test.js 如下
import d from './demo'
console.log(d) // {a: 1}
import {default as d1,other1, other2 as myName, other3} from './demo'
console.log(d1) // {a: 1}
console.log(other1) // {b: 2, c: 3}
console.log(myName) // {d: 4}
console.log(other3) // undefined
import * as all from './demo'
console.log(all) // {default: Object, other1: Object, other2: Object, __esModule: true}
console.log(all.default) // {a: 1}
注意: import
和 export
只能在顶级用,不能在代码块中用。否则会报 'import' and 'export' may only appear at the top level
。例如
var isDebugger = true
if(isDebugger) {
import mock from './mock'
}
这种情况,可以考虑用 CommonJS 或 System.import 的写法。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有