1

我正面临这个编译时错误

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
      })
    );
};
4

3 回答 3

3

是的,第一次学习 React 的人犯的错误。

要使用在其他文件中定义的函数,您

1.从定义的文件中导出函数。2.在新文件中导入函数。

它可以通过两种方式完成。

语法 1:

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

当 then 是要导出的多个函数/常量时,请使用上述语法。

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

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

语法 2:

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

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

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

您现在可以像这样导入。

import function1 from <file-name>
于 2018-11-07T10:32:26.167 回答
2

您需要导入createProfile 单个导入

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

或将其导出为默认导出

export default createProfile
于 2018-11-07T10:19:38.330 回答
1

将 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;
于 2018-11-07T14:11:06.007 回答