history.replace()在auth0类中不起作用
//src/history.js
import { createBrowserHistory } from 'history';
export default createBrowserHistory();
然后,我尝试将它导入到我的Auth0类中,以使用history.replace()。这是来自类的两个函数,以及我如何尝试使用这个函数。
//components/Auth.js
handleAuthentication() {
this.auth0.parseHash((err, authResult) => {
if (authResult && authResult.accessToken && authResult.idToken) {
this.setSession(authResult);
} else if (err) {
console.log(err);
alert(`Error: ${err.error}. Check the console for further details.`);
location.hash = "";
//location.pathname = "/";
history.replace("/");
}
});
}
setSession(authResult) {
// Set isLoggedIn flag in localStorage
//localStorage.setItem('isLoggedIn', 'true');
// Set the time that the access token will expire at
let expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
localStorage.setItem("access_token", authResult.accessToken);
localStorage.setItem("id_token", authResult.idToken);
localStorage.setItem("expires_at", expiresAt);
location.hash = "";
//location.pathname = "/dashboard";
history.replace("/dashboard");
}
我还在这里将历史作为道具添加到我的路由器中。
<Router history={history}>
<Switch>
<Route exact path="/" render={(props) => <Home auth={this.props.auth} {...props} />}/>
<Route path="/dashboard" render={(props) => <Dashboard auth={this.props.auth} {...props} />}/>
<Route path="/callback" render={(props) => <Callback auth={this.props.auth} {...props} />}/>
</Switch>
</Router>
我读过用withRouter()包装组件,但这似乎只对组件有效,在这种情况下,我只是尝试使用一个类。
发布于 2019-04-23 05:54:09
在运行时,您是否在控制台中看到错误?这似乎与Auth0没有直接关系,更多的是在调用时填充到顶部的道具方面。谢谢!
https://stackoverflow.com/questions/55784336
复制相似问题