系统在一个用户操作过程中,保留功能的上一次操作数据,但是退出登录后,换个用户登录进来,发现还存在上个用户的数据,怎么一个语句清除整个store中的数据?
还有一个问题,我的系统路由用了嵌套,访问:http://localhost:8000/main/repos,这个时候f5刷新的时候,整个页面空白,控制台:Uncaught SyntaxError: Unexpected token <,index.js:1,然后在控制台上都看不到源代码了...
@bjwulin 刚解决这个问题。按照 @sorrycc 大大说的,用onReducer。我的理解是onReducer能够截获全局的action和state,并返回新的state。在我们这个例子里,如果action是logout,就返回最初的state,如果action不是logout,那么接收到什么state就返回什么state。
现在的问题是怎么获得最初的state。当然,我们可以直接在index.js里面再写一遍最初的state。不过,随着业务的变化,想要修改几个state的话,还得来index.js也把相应的初始状态改变了,很麻烦。所以直接从models里导入比较方便。
代码如下:
const models=[];
models.push(require('./models/exhibit'));
models.push(require('./models/user'));
const initialState={};
models.forEach((m)=>initialState[m.namespace]=m.state);
const undo = r => (state, action) => {
const newState = r(state, action);
if (action.type == 'user/logoutOnlyStatus') {
return {
routing: newState.routing,
...initialState
}
}
else {
return newState
}
};
const app=dva({
onError(e){
console.log(e)
},
onReducer: undo
});
app.model(require('./models/exhibit'));
app.model(require('./models/user'));
app.router(require('./router'));
app.start('#root');
@zhangdistephen 我用onReducer的时候是报错的。错误是读取不到store的值。我的是异步加载model,换成同步貌似也不可以
Most helpful comment
@bjwulin 刚解决这个问题。按照 @sorrycc 大大说的,用onReducer。我的理解是onReducer能够截获全局的action和state,并返回新的state。在我们这个例子里,如果action是logout,就返回最初的state,如果action不是logout,那么接收到什么state就返回什么state。
现在的问题是怎么获得最初的state。当然,我们可以直接在index.js里面再写一遍最初的state。不过,随着业务的变化,想要修改几个state的话,还得来index.js也把相应的初始状态改变了,很麻烦。所以直接从models里导入比较方便。
代码如下: