导入功能时反应错误

时间:2018-11-07 10:15:20

标签: javascript reactjs react-redux react-router

我在此遇到编译时错误

import createProfile from "../../actions/profileActions";

跟随错误 编译失败。

[1]
[1] ./src/components/create-profile/CreateProfile.js
[1] 424:57-70 "export 'default' (imported as 'createProfile') was not found in '../../actions/profileActions

但要导入的功能在profileActions.js中可用

export const createProfile = (profileData, history) => dispatch => {
  axios
    .post("/api/profile", profileData)
    .then(res => history.push("/dashboard"))
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    );
};

3 个答案:

答案 0 :(得分:2)

您需要导入createProfile一次导入

import {createProfile} from "../../actions/profileActions";

或将其导出为默认导出

export default createProfile

答案 1 :(得分:2)

是的,React的初学者会犯的错误。

要使用其他文件中定义的功能

1。从定义的文件中导出函数。 2.将该功能导入新文件。

可以通过两种方式完成。

语法1:

export function someFunction(){}
export const constant1="ABC"

当您要导出多个函数/常量时,请使用以上语法。

在这种情况下,如果要导入,请遵循以下语法。

import {function1,constant1,...} from <file-name>

语法2:

默认导出每个文件只能使用一个,即您不能以默认方式导出两个函数/常量

export default function1=()=>{}//okay!!

export default function1=()=>{};
export default const someconstant;//ERROR !!

您现在可以像这样导入。

import function1 from <file-name>

答案 2 :(得分:0)

将createProfile导入为import {createProfile} from "../../actions/profileActions";,否则将其导出为

const createProfile = (profileData, history) => dispatch => {
  axios
    .post("/api/profile", profileData)
    .then(res => history.push("/dashboard"))
    .catch(err =>
      dispatch({
        type: GET_ERRORS,
        payload: err.response.data
      })
    );
};
export default createProfile;