【问题标题】:Error while importing Named export [duplicate]导入命名导出时出错[重复]
【发布时间】:2018-01-20 19:40:35
【问题描述】:

我有 helperMethods.js 文件

export default function calculateWinner(squares) {
    const lines = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
        [0, 4, 8],
        [2, 4, 6],
    ];
    for (let i = 0; i < lines.length; i++) {
        const [a, b, c] = lines[i];
        if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
            return squares[a];
        }
    }
    return null;
}

我知道在这种情况下我只能导出一种方法。

我在我导入的文件中使用此方法:

import calculateWinner from './helperMethods.js';

并像这样使用:

render() {
    const history = this.state.history;
    const current = history[this.state.stepNumber];
    const winner = calculateWinner(current.squares);
    // some more code here
}

在这种情况下,这是可行的。

但如果我尝试将默认导出更改为命名导出:

function calculateWinner(squares) {
    const lines = [
        [0, 1, 2],
        [3, 4, 5],
        [6, 7, 8],
        [0, 3, 6],
        [1, 4, 7],
        [2, 5, 8],
        [0, 4, 8],
        [2, 4, 6],
    ];
    for (let i = 0; i < lines.length; i++) {
        const [a, b, c] = lines[i];
        if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) {
            return squares[a];
        }
    }
    return null;
}

export {calculateWinner};

我收到错误:

game.js:50 Uncaught TypeError: (0 , _helperMethods2.default) is not a function

同意这个错误非常简单,但我应该如何修改我的导出/导入以使这个命名导出工作?

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    使用命名导入而不是默认导入语法。

    import { calculateWinner } from './helperMethods.js';
    

    【讨论】:

      猜你喜欢
      • 2021-09-14
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 2019-01-02
      • 2013-11-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-19
      相关资源
      最近更新 更多