node中导入模块:var 名称 = require('模块标识符')node
node中向外暴露成员的形式:module.exports = {}es6
在ES6中,也经过规范的形式,规定了ES6中如何导入和导出模块ui
ES6中导入模块,使用 import 模块名称 from '模块标识符' import '表示路径'code
import *** from *** 是ES6中导入模块的方式it
例如console
// test.js export default { name: 'zs', age: 20 }
或者test
// test.js var info = { name: 'zs', age: 20 } export default info
在 main.js 中 接收 test.js 使用 export default 向外暴露的成员import
import person from './test.js' console.log(person) // {name: 'zs', age: 20}
注意:
一、export default 向外暴露的成员,能够使用任意变量来接收require
二、在一个模块中,export default 只容许向外暴露一次变量
三、在一个模块中,能够同时使用export default 和export 向外暴露成员
四、使用export向外暴露的成员,只能使用{ }的形式来接收,这种形式,叫作【按需导出】
五、export能够向外暴露多个成员,同时,若是某些成员,在import导入时,不须要,能够不在{ }中定义
六、使用export导出的成员,必须严格按照导出时候的名称,来使用{ }按需接收
七、使用export导出的成员,若是想换个变量名称接收,能够使用as来起别名
例如:
// test.js var info = { name: 'chuhx', age: 19 } export default info export var title = '小星星' export content = 'hello world'
在main.js中接收 test.js 使用 export defalut 和 export 向外暴露的成员
import person, {title, content as content1} from './test.js' console.log(person) // { name: 'chuhx', age: 19} console.log(title + '======' +content1) // 小星星 =======hello world