es6 import export 引入导出变量方式

 

1
2
3
4
5
6
var testdata='sdfkshdf';
//export testdata;//err
export {testdata as ms};
export var firstName = 'Michael';
export var lastName = 'Jackson';
export var year = 1958;

  

1
2
3
4
import $ from 'jquery';
import lastName from './index2.js';
import {ms} from './index2.js';
console.log(ms,'2')

  export {testdata as ms}; 这样的导出方式,意思是导出文件接口的子对象到外面

  直接//export testdata;//err 这种方式是会报错的,因为不能直接导出变量,需要用一个接口来继承变量然后导出,比如 export var firstName = 'Michael'; firstname 就是接口

1
2
var testdata='sdfkshdf';
export var abc=testdata;  //var一个接口abc,然后这个接口继承一个变量的值,然后导出,然后外面直接访问abc即可得到testdata的值了

  

1
export {a,b,c} //这种方式代表导出a,b,c 作为文件接口的子对象导出到外面

  

1
2
3
4
import $ from 'jquery';
import lastName from './index2.js'; 导入模块的lastName的接口
import {ms} from './index2.js'; //导入模块的子对象ms
console.log(ms,'2')

  

1
2
3
4
5
6
7
8
9
var data='sdflsdfjsdjf';
export default data;
 
 
 
/import:
 
import data from './index2.js';
console.log(data,'2')

  上面这样的输出方式是正确的方式。

如果:

1
export default data='sdfsdjfdf';

  这样的方式是错误的,外面不能得到data定义的值,且data定义的值也不会生效。

原文地址:https://www.cnblogs.com/sxz2008/p/7093258.html