在登录后,react-redux重定向到其他页面 [英] react-redux redirect to other page after login
本文介绍了在登录后,react-redux重定向到其他页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
action.js
:
export const login = creds => {
console.log(`${url}/login`);
const requestOptions = {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/json"
},
body: creds
};
return function(dispatch) {
dispatch({ type: LOGIN_REQUEST });
function timer() {
return fetch(`${url}/login`, requestOptions).then(response => {
if (!response.ok) {
console.log(response);
return response.json().then(json => {
var error = new Error(json.message);
error.response = response;
throw error;
});
} else {
console.log("3");
return response.json();
}
}).then(user => {
if (user.message === "ok") {
localStorage.setItem("token", user.token);
dispatch({ type: LOGIN_SUCCESS, payload: user.token });
window.location.href = `${app}/dashboard`;
} else {
const error = user.message;
throw new Error(error);
}
}).catch(function(error) {
dispatch(loginError(error));
});
}
setTimeout(timer, 5000)
}
};
我无法以单页方式重定向到我的仪表板,我搜索了很多但我没有得到任何有用的东西。我正在使用React Router v4。能否请您建议我是否以正确的方式使用JWT进行此用户登录。
I am unable to redirect in a single-page manner to my dashboard, I searched a lot but I didn't get anything useful. I am using React Router v4. Can you please suggest whether I am doing this user login with JWT the right way or not.
推荐答案
创建您自己的 history
在 history.js
文件中使用此历史库。
create a your own history
in history.js
file using this history library.
//history.js
import createHistory from 'history/createBrowserHistory'
const history = createHistory()
export default history
将其提供给您的路由器:
Supply it to your router:
<Router history = {history}>.....</Router>
然后你可以使用这个历史
对象来从任何地方重定向。在你的行动中:
Then you can use this history
object to redirect from anywhere. In your action:
import history from './history'
history.push(`${app}/dashboard`)
这篇关于在登录后,react-redux重定向到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文