reactjs 为什么我得到错误模块未找到:无法解析“redux”

fquxozlt  于 2023-06-05  发布在  React
关注(0)|答案(4)|浏览(254)

我是新的redux我已经创建了一个演示,其中我有安装Reactredux和redux
文件夹结构

src
    js
      store
        index.js
      action
        index.js
      reducers
        index.js
      index.js  ///////**2**
  index.js ///////////  **1**
  App.js .... etc

1index.js中,我有以下代码

import index from "./js/index";

2index.js中,我有以下代码

import store from "../js/store/index";
   import { addArticle } from "../js/actions/index";

   window.store=store;
   window.addArticle=addArticle;

当我npm启动时,我得到以下错误;

./src/js/store/index.js
   Module not found: Can't resolve 'redux' in 'E:\reacr-redux\src\js\store'

js/store/index.js

import { createStore } from "redux";
     import rootReducer from "../reducers/index";

     const store=createStore(rootReducer);

     export default store;

reducers/index.js

import { ADD_ARTICLE } from "../constants/action-types";
 const initialState={
articles:[]
 };
  const rootReducer= ( state = initialState ,  action ) => {
    switch(action.type){
    case ADD_ARTICLE:
    state.articles.push(action.payload);
    return state;
    default :
    return state;
    }
   };
    export default rootReducer;

package.json

{
"name": "reacr-redux",
"version": "0.1.0",
 "private": true,
 "dependencies": {
"font-awesome": "^4.7.0",
"mdbootstrap": "^4.5.15",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-redux": "^5.1.1",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1"
 },
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",

 },
 "eslintConfig": {
   "extends": "react-app"
  },
 "browserslist": [
  ">0.2%",
  "not dead",
  "not ie <= 11",
  "not op_mini all"
  ],
 "devDependencies": {
   "redux": "^4.0.1"
   }
  }

有谁能告诉我发生了什么事吗?我是新来的,我哪里错了

pkln4tw6

pkln4tw61#

不要在Redux中使用save-dev。从devDependencies中删除:

npm uninstall redux --save-dev

并使用以下命令重新安装:

npm i redux --save
ctehm74n

ctehm74n2#

只需安装react工具包就可以解决这个问题。

# NPM
npm install @reduxjs/toolkit

# Yarn
yarn add @reduxjs/toolkit
2j4z5cfb

2j4z5cfb3#

运行 npm install @reduxjs/toolkit 后,尝试重启服务器。

ssm49v7z

ssm49v7z4#

使用此命令安装React Redux npm install @reduxjs/toolkit

相关问题