const
就好像let
, it is a 词法声明 http://www.ecma-international.org/ecma-262/6.0/#sec-let-and-const-declarations (变量声明、声明) 用于在块中定义标识符。
你正试图将其与default
关键词,它期望一个可提升声明、类声明 or 赋值表达式 http://www.ecma-international.org/ecma-262/6.0/#sec-exports-static-semantics-boundnames跟随它。
因此它是一个语法错误.
如果你想const
您需要提供标识符而不是使用的东西default
.
export
本身接受一个变量语句 or 宣言在它的右边。
下面的就好了export default Tab;
Tab
becomes an AssignmentExpression as it's given the name default ?
export default Tab = connect( mapState, mapDispatch )( Tabs );
is fine
Here Tab = connect( mapState, mapDispatch )( Tabs );
is an 赋值表达式.
Update:以不同的方式想象问题
如果您试图从概念上理解这一点并且上面的规范推理没有帮助,请将其视为"if default
是一个合法的标识符而不是保留的令牌,有什么不同的写法export default Foo;
and export default const Foo = 1;
?"
在这种情况下,扩展的写法是
// pseudocode, this thought experiment is not valid JS
export default Foo;
// would be like
export const default = Foo;
export default const Foo = 1;
// would be like
export const default const Foo = 1;
// so would the following line make sense?
const bar const Foo = 1;
扩展有一个有效的论据should是这样的
// pseudocode, this thought experiment is not valid JS
export default const Foo = 1;
// would be like
const Foo = 1;
export const default = Foo;
然而,这将变得模棱两可谢尔盖的评论 https://stackoverflow.com/questions/36261225/why-is-export-default-const-invalid/36261387?noredirect=1#comment80008351_36261387,因此显式编写此模式更有意义。