在React应用中收到“尝试导入错误:”


117

尝试运行React应用时收到以下错误:

./src/components/App/App.js
尝试导入错误:'combineReducers'
不是从'../../store/reducers/'导出的。

这是我的导出方式combineReducers

import { combineReducers } from 'redux';
import userReducers from './userReducers';
import articleReducers from './articleReducers';

export default combineReducers({
    userReducers,
    articleReducers
});

这就是我如何导入它App.js

import { combineReducers } from '../../store/reducers';

我的出口方式有什么不正确的地方combineReducers

Answers:


244

import { combineReducers } from '../../store/reducers';

应该

import combineReducers from '../../store/reducers';

因为它是默认导出,而不是命名导出。

还有的两者之间的差异的良好击穿这里


1
感谢您将默认出口与命名出口区分开来
弗朗西斯·培根

不幸的是,关于差异细分的博客文章已损坏。
sboisse

15

我遇到了同样的问题,但我只是export在顶部输入并在底部删除了默认值。向下滚动并查看注释。

import React, { Component } from "react";

export class Counter extends Component { // type this  
export default Counter; // this is eliminated  

4

我想我来晚了,但是此信息可能对我发现某事的任何人有用,这可能很简单但很重要。如果直接在函数上使用导出,即

export const addPost = (id) =>{
  ...
 }

请注意,导入时需要将其用大括号括起来,即 import {addPost} from '../URL';

但是当使用导出默认值即

const addPost = (id) =>{
  ...
 }

export default addPost

然后,您可以不带花括号导入,即 import addPost from '../url';

export default addPost

希望这对像我一样感到困惑的人有所帮助。🙂


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.