Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

系统退出登录后,怎么一个语句清除store中的所有数据,以及嵌套路由刷新页面时报错。 #578

Closed
bjwulin opened this issue Feb 9, 2017 · 3 comments
Labels

Comments

@bjwulin
Copy link

bjwulin commented Feb 9, 2017

系统在一个用户操作过程中,保留功能的上一次操作数据,但是退出登录后,换个用户登录进来,发现还存在上个用户的数据,怎么一个语句清除整个store中的数据?

还有一个问题,我的系统路由用了嵌套,访问:http://localhost:8000/main/repos,这个时候f5刷新的时候,整个页面空白,控制台:Uncaught SyntaxError: Unexpected token <,index.js:1,然后在控制台上都看不到源代码了...

@sorrycc
Copy link
Member

sorrycc commented Feb 10, 2017

怎么一个语句清除整个store中的数据?

onReducer 截获 action

然后在控制台上都看不到源代码了...

index.js 改成 /index.js

@zhangdistephen
Copy link

zhangdistephen commented Mar 29, 2017

@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');

@nai6514531
Copy link

@zhangdistephen 我用onReducer的时候是报错的。错误是读取不到store的值。我的是异步加载model,换成同步貌似也不可以

@sorrycc sorrycc closed this as completed Sep 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants