詳解node和ES6的模組匯出與匯入
阿新 • • 發佈:2020-02-20
node的匯出與匯入
1.node的匯出語法
var path = {} module.exports = path
2.node的匯入語法
const path = require('path')
ES6的匯出與匯入
1.ES6的匯出語法
export default {} (只能暴漏一個成員) export var s = {} export var b = {} (可以暴漏多個成員)
2.ES6的匯入語法
import a from '包名(或者是檔案路徑)' (匯入export default暴漏的成員) import {s} from '包名(或者是檔案路徑)' (匯入export 暴漏的成員,名字要一致)
例項補充:
//普通匯出 export { name:'zs',age: 20 } //預設匯出的模組,讓使用者自己命名,且只能使用default匯出一次。 export default { name:'zs',age: 20 } //匯出的其他模組 export var title = "小星星" export var content = "哈哈哈"
匯入例項
import {name,age} from './test.js import m1 from './test.js import {title,content} from './test.js' console.log(m1) console.log(title+"------"+content)
以上就是本次介紹的全部相關內容,感謝大家的學習和對我們的支援。