ES6 模块导入import 导出export 和module.export

ES6中新增了模块的导入和导出功能

在实际过程中可以使用 import 和 export 对模块进行导入和导出操作,具体如下

1. 名字导入/导出  (导入名字必须与导出的一致,导入时需要用花括号)

//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}
export function add (x, y) {
    return x + y;
}

2 . 导入时也可以用 * ,导入整个文件

//------ main.js ------
import * as lib from lib.js
console.log(lib.sqrt)
console.log(lib.add)

3. 默认导出,每个模块可以有一个默认导出,这样导入时的名字可以与导出不一致

/* Store实例 */
export default new Vuex.Store({
    state,
    getters,
    actions,
    mutations
})
import store from './store/'

 在 import 的时候 如果不使用相对路径或者绝对路径,node默认会去node_modules/文件夹下去找

posted @ 2018-03-16 01:37  吃饭睡觉打豆豆o  阅读(617)  评论(0编辑  收藏  举报