问题描述
我认为以下内容很好:
const Tab = connect( mapState, mapDispatch )( Tabs );导出默认选项卡;
然而,这是不正确的:
export default const Tab = connect( mapState, mapDispatch )( Tabs );
不过这很好:
export default Tab = connect( mapState, mapDispatch )( Tabs );
能否解释一下为什么const
对export default
无效?它是一个不必要的添加 &任何声明为 export default
的东西都假定为 const
之类的?
const
就像 let
, 它是一个 LexicalDeclaration (VariableStatement, Declaration) 用于在块中定义标识符.
您试图将其与 default
关键字混合使用,它期望 HoistableDeclaration、ClassDeclaration 或 AssignmentExpression 跟随它.>
因此它是一个SyntaxError.
如果你想const
,你需要提供标识符而不是使用default
.
export
本身接受一个 VariableStatement 或 Declaration 在它的右边.
以下就可以了export default Tab;
Tab
变成了 AssignmentExpression,因为它的名字是 default
export default Tab = connect( mapState, mapDispatch )( Tabs );
没问题
这里Tab = connect( mapState, mapDispatch )( Tabs );
是一个AssignmentExpression.
更新:想象问题的不同方式
如果您试图从概念上理解这一点并且上面的规范推理没有帮助,请将其视为如果 default
是合法标识符而不是保留标记,编写 export default Foo;
和 export default const Foo = 1;
的不同方式是什么?"
在这种情况下,扩展的编写方式是
//伪代码,这个思想实验是无效的JS导出默认 Foo;//会像导出常量默认值 = Foo;导出默认 const Foo = 1;//会像导出 const 默认 const Foo = 1;//那么下面这行有意义吗?const bar const Foo = 1;
有一个有效的论证,扩展应该类似于
//伪代码,这个思想实验是无效的JS导出默认 const Foo = 1;//会像常量 Foo = 1;导出常量默认值 = Foo;
但是,根据 Sergey 的说法,这会变得模棱两可注释,所以明确地写这个模式更有意义.
I see that the following is fine:
const Tab = connect( mapState, mapDispatch )( Tabs );
export default Tab;
However, this is incorrect:
export default const Tab = connect( mapState, mapDispatch )( Tabs );
Yet this is fine:
export default Tab = connect( mapState, mapDispatch )( Tabs );
Can this be explained please why const
is invalid with export default
? Is it an unnecessary addition & anything declared as export default
is presumed a const
or such?
const
is like let
, it is a LexicalDeclaration (VariableStatement, Declaration) used to define an identifier in your block.
You are trying to mix this with the default
keyword, which expects a HoistableDeclaration, ClassDeclaration or AssignmentExpression to follow it.
Therefore it is a SyntaxError.
If you want to const
something you need to provide the identifier and not use default
.
export
by itself accepts a VariableStatement or Declaration to its right.
Tab
becomes an AssignmentExpression as it's given the name default
Here Tab = connect( mapState, mapDispatch )( Tabs );
is an AssignmentExpression.
Update: A different way to imagine the problem
If you're trying to conceptually understand this and the spec-reasoning above is not helping, think of it as "if default
was a legal identifier and not a reserved token, what would be a different way to write export default Foo;
and export default const Foo = 1;
?"
In this situation, the expanded way to write it would be
// 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;
There is a valid argument the expansion should be something like
// pseudocode, this thought experiment is not valid JS
export default const Foo = 1;
// would be like
const Foo = 1;
export const default = Foo;
However, this then would become ambiguous per Sergey's comment, so it makes more sense to write this pattern explicitly instead.
这篇关于为什么“导出默认常量"无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!