我按照官方文档中的说明进行操作;我的应用在 chrome 中运行良好,指向localhost:3000
. 但是,现在我遇到了一个问题,因为似乎如果浏览器没有 Redux 扩展,它将无法工作。根据文档,我写了:
import { createStore, applyMiddleware, compose } from "redux";
import thunk from "redux-thunk";
import rootReducer from "./reducers"; //we dont have to put index.js because its called index.js
const initialState = {};
const middleware = [thunk];
const store = createStore(
rootReducer,
initialState,
compose(
applyMiddleware(...middleware),
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
)
); //spread operator '...'
export default store;
我错过了什么吗?似乎其他人也遇到了同样的问题,并且每个代码库的解决方案似乎都是独一无二的。
编辑:我也试过这个解决方案,但我得到一个错误TypeError: _store__WEBPACK_IMPORTED_MODULE_12__.default.dispatch is not a function
编辑 2:在 chrome 隐身模式下(未启用 redux 开发工具扩展),我遇到了错误,
TypeError: Cannot read property 'apply' of undefined
(anonymous function)
C:/Users/MishaFresh/Desktop/mern-app/MERN_app/client/node_modules/redux/es/redux.js:575
572 |
573 | return funcs.reduce(function (a, b) {
574 | return function () {
> 575 | return a(b.apply(void 0, arguments));
576 | };
577 | });
578 | }
同样的错误在 Firefox 中。
我的package.json
:
"scripts": {
"client-install": "npm install --prefix client",
"start": "node server.js",
"server": "nodemon server.js",
"client": "npm start --prefix client",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "NPM_CONFIG_PRODUCTION=false npm install --prefix client && npm run build --prefix client"
},
编辑3:
实施以下建议后的新错误:
TypeError: _store__WEBPACK_IMPORTED_MODULE_12__.default.dispatch is not a function
Module../src/App.js
C:/Users/Me/Desktop/my-app/app/client/src/App.js:40
37 | //decode it
38 | const decoded = jwt_decode(localStorage.jwtToken);
39 | //set user and isAuthenticated
> 40 | store.dispatch(setCurrentUser(decoded));
| ^ 41 |
42 | //check for expired token
43 | const currentTime = Date.now() / 1000;