我正在学习redux工具包,不幸的是,我遇到了像Type Error这样的错误,但我没有做出解决方案.
创建计数器切片
const { createSlice } = require("@reduxjs/toolkit");
const initialState = {
count: 0,
};
const counterSlice = createSlice({
name: "counter",
initialState,
reducers: {
increment: (state, action) => state.count++,
decrement: (state, action) => state.count--,
},
});
module.exports = counterSlice.reducer;
module.exports.counterActions = counterSlice.reducer;
配置Redux Store
const { configureStore } = require("@reduxjs/toolkit");
const counterReducer = require("../features/counter/counterSlice");
// Configure Store
const store = configureStore({
reducer: {
counter: counterReducer,
},
});
module.exports = store;
派送Redux Store
const store = require("./src/rtk/store/store");
const counterActions = require("./src/rtk/features/counter/counterSlice");
console.log(store.getState());
// subscribe to state chnages
store.subscribe(() => {
console.log(store.getState());
});
// dispatch Actions
store.dispatch(counterActions.increment());
store.dispatch(counterActions.decrement());
我面对的错误大约是
TypeError: counterActions.increment is not a function
,如下所示,
[nodemon] restarting due to changes...
[nodemon] starting `node app.js`
{ counter: { count: 0 } }
/media/imdad/Programming/MERN/Redux/Redux-Toolkit/rtkBasicSetup/app.js:12
store.dispatch(counterActions.increment());
^
TypeError: counterActions.increment is not a function
at Object.<anonymous> (/media/imdad/Programming/MERN/Redux/Redux-Toolkit/rtkBasicSetup/app.js:12:31)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
at node:internal/main/run_main_module:23:47
Node.js v18.17.0
Can anyone please help me to solve this issue? The error is also given in the attached file please check it.