码迷,mamicode.com
首页 > 其他好文 > 详细

import、export 和 export default

时间:2019-07-23 15:07:10      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:imp   div   efault   const   著作权   get   list   ice   http   

ES6中
在JavaScript ES6中,export与export default均可用于导出常量、函数、文件、模块等。
你可以在其它文件或模块中通过import+(常量 | 函数 | 文件 | 模块)名的方式,将其导入,以便能够对其进行使用,但在一个文件或模块中,export、import可以有多个,export default仅有一个。
1.export
export const str = hello world
export function f(a){
    return a+1
}

对应的导入方式:
import { str, f } from demo1 //也可以分开写两次,导入的时候带花括号

2.export default
export default const str = hello world

对应的导入方式:
import str from demo1 //导入的时候没有花括号


注意
1.import引入一个依赖包,不需要相对路径。import 引入一个自己写的js文件,是需要相对路径的。
示例:import axios from ‘axios’;
import AppService from ‘./appService’;
2.用export抛出的变量,import要用{}的方式引入,使用export default抛出的变量,只需要一个变量就行。
(1)
使用export抛出的变量需要用{}进行import:
//a.js
export function getList(){};
//b.js
import { getList } from ‘./a.js’;
(2)
使用export default抛出的变量,只需要自己起一个名字就行:
//a.js
var obj = { name: ‘xiu’ };
export default obj;
//b.js
import aaa from ‘./a.js’;
console.log(aaa.name);   // ‘xiu’;
3.一个js文件中,只能有一个export default; 但是,一个js文件中,可以有多个export。

作者:阿r阿r
链接:https://www.jianshu.com/p/5e6a540b3344
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。

 

import、export 和 export default

标签:imp   div   efault   const   著作权   get   list   ice   http   

原文地址:https://www.cnblogs.com/Jeely/p/11231458.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!