【问题标题】:"Attempted import error: 'authReducer' is not exported from './authReducer'. "“尝试导入错误:'authReducer' 未从 './authReducer' 导出。”
【发布时间】:2019-06-15 10:30:20
【问题描述】:

**实际上,我正在尝试从名为 authReducer.js 的 JS 文件中导入一个函数,但我无法这样做。

在 authReducer.js 中,我尝试将该函数以 authReducer 的名称存储在 const 变量中,然后尝试将其导出,但仍然出现同样的错误

index.js

import {combineReducers} from 'redux'

import {authReducer} from './authReducer' //I'm getting error here

export default combineReducers({
    auth:authReducer
});

authReducer.js

const initialState={
    isAuthenticated:false,
    user:{}
}


export default function(state=initialState,action){
    switch(action.type)
    {
        default:
            return state;
    }
};

两者都位于同一个文件夹中。

【问题讨论】:

    标签: reactjs redux react-redux


    【解决方案1】:

    从以下位置更改您的导入:

    import { authReducer } from './authReducer'
    

    到:

    import authReducer from './authReducer`'
    

    【讨论】:

    • @AvinasshBharadhwaj ;)
    【解决方案2】:

    默认导出不需要大括号,它们用于命名导出。

    /*  somewhere.js  */
    const Potato = 5;
    const Chernobyl = "hello";
    
    export { Potato };
    export default Chernobyl;
    

    然后在导入时:

    import Chernobyl, { Potato } from "somewhere";
               ^           ^
           default       named
    

    【讨论】:

      【解决方案3】:

      您的导入语句应如下所示,因为您的函数是默认导出。仅当它是命名导出时才使用 {}。

      import authReducer from './authReducer'
      

      【讨论】:

        猜你喜欢
        • 2021-01-24
        • 2020-07-16
        • 1970-01-01
        • 2019-06-06
        • 2020-06-26
        • 2019-10-24
        • 2021-10-20
        • 2019-12-19
        • 2023-03-11
        相关资源
        最近更新 更多