我正在使用ReactJS,MobX和react-router v4進行網絡應用程序,並且我在路由器歷史和重定向方面遇到了一些問題。 我想重新定向用戶的主頁時,他將註銷或登錄,我試圖在我的商店實現這一點。這裏是源代碼:ReactJS MobX和react-router v4與url歷史問題
index.js
const APP = document.getElementById('app');
render(
(
<Provider {...stores} >
<MuiThemeProvider>
<Router history={history} >
<div>
<Routes />
</div>
</Router>
</MuiThemeProvider>
</Provider>
) , APP);
Routes.js
@inject('userStore', 'commonStore')
@withRouter
@observer
class Routes extends Component {
render() {
return (
<div>
</div>
<switch>
<Route exact path="/create_listing" render={() =>
(<CreateListing store={clStore} />)}
/>
<Route path="/create_listing/description" render={() =>(
<CLLayout store={clStore} />
)}/>
<Route path="/create_listing/amenities" render={() =>(
<CLLayout store={clStore} />
)}/>
<Route path="/create_listing/optional" render={() =>(
<CLLayout store={clStore} />
)}/>
<Route path="/create_listing/media" render={() =>(
<CLLayout store={clStore} />
)}/>
<Route path="/create_listing/preview" render={() =>(
<CLLayout store={clStore} />
)}/>
<Route path="/create_listing/done" render={() =>(
<CLLayout store={clStore} />
)}/>
<Route exact path="/" component={ FrontPageIndex }/>
</switch>
</div>
);
}
}
export default Routes;
history.js
import createBrowserHistory from 'history/createBrowserHistory';
import createMemoryHistory from 'history/createMemoryHistory';
export default process.env.BROWSER? createBrowserHistory() : createMemoryHistory();
個store.js
@action logout() {
return agent.Auth.logout()
.catch(action((err) => {
this.errors = err.response && err.response.body && err.response.body.errors;
throw err;
}))
.finally(action(() => {
commonStore.setToken({});
userStore.forgetUser();
this.inProgress = false;
history.replace('/');
}));
}
方法history.replace( '/');實際上,替換瀏覽器中的URL,但不呈現新組件(家庭組件),爲此我應手動刷新網頁。
對此有何建議?
同樣的問題..你找出一個解決方案,以取代歷史刷新網頁? – Wonka
是的,我只是給一個答案。 – pureofpure