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