【问题标题】:Why importing with curly brace causes issue?为什么用花括号导入会导致问题?
【发布时间】:2019-04-23 00:44:08
【问题描述】:

我已经检查了this question 关于带/不带花括号导入之间的区别。

就我而言,我有一个班级:

class PathResolver extends Component {
//content
}
export default connect()(PathResolver);

如果我使用
import { PathResolver } from '/path';
而不是
import PathResolver from '/path';
它会抱怨:Attempted import error: 'PathResolver' is not exported from '/path'.

当类名是“PathResolver”时,这两个import语句应该是一样的,但是大括号的import语句好像找不到连接的“PathResolver”,为什么?

【问题讨论】:

    标签: reactjs redux react-redux react-router


    【解决方案1】:

    如果你也导出类,你也可以这样做。

    export class PathResolver extends Component {
    //content
    }
    

    由于当前您的组件被包裹在 redux 周围,您将无法访问它

    【讨论】:

    • 你的意思是export class PathResolver?在这种情况下,带有花括号的那个 (import { PathResolver } from '/path';) 将与 redux 相关的功能有问题,例如this.props.dispatch() is not defined。似乎是在导入纯类而不是连接类。
    • @PhoenixPan,它将导出纯组件,而不是基于 redux 的功能。它基本上是为单元测试而完成的。你能解释一下你的用例吗?
    • 是的,我希望保持 store 连接并导出连接的组件...似乎无法使用import { PathResolver } from '/path' 连接组件,但我不知道为什么...
    • 伙计,这是一种奇怪的做法,但您可以将连接的组件分配给一个变量,然后执行 2 个导出
    • 哈哈,我应该简单地在组件中使用default 关键字来做import PathResolver from '/path'。只是好奇是否可以显式导入连接的组件:p import { connect()(PathResolver) } from '/path'; 肯定是错误的......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 2016-08-16
    相关资源
    最近更新 更多