出口预期的声明或声明

时间:2017-11-28 06:48:26

标签: javascript ecmascript-6

为什么我能够这样做:

export const state = {};

但我不能这样做:

const state = {}
export state; // 'Declaration or statement expected' error

1 个答案:

答案 0 :(得分:2)

所以你有2路使用导出: 你已经完成了这个:

export const state = /*whatever you want*/

这一个:

const state = /*whatever you want*/
export { state };

然后你可以用它:

import { state } from './otherFileName'

引自documentation

  

<强>语法

export { name1, name2, …, nameN };
export { variable1 as name1, variable2 as name2, …, nameN };
export let name1, name2, …, nameN; // also var, function
export let name1 = …, name2 = …, …, nameN; // also var, const