好吧,我的 React-Router 或多或少按照我想要的方式工作,但我的问题是:我现在需要在 JS 中添加一些功能。
示例:用户输入详细信息并单击“登录”,我希望我的 jQuery 检查凭据是否正确以及是否更改了主页的路由。
我不知道该怎么做。
这是我的 React-Router 代码:
import React from 'react'
import ReactDOM from 'react-dom';
import { Router, Route, hashHistory, Navigation } from 'react-router'
import Login from './login'
import Signup from './signup'
import Home from './home'
ReactDOM.render((
<Router history={hashHistory}>
<Route path="/" component={Login}/>
<Route path="/signup" component={Signup}/>
<Route path="/home" component={Home}/>
</Router>
), document.getElementById("app"))
$("#loginBtn").click(
function(){
var email = $('loginEmail').val();
var pass = $('loginpwd').val();
/* Check to see if credential are correct.
* If so, change to '/home route
*/
}
);
任何想法将不胜感激。谢谢:)
最佳答案
一种方法如下:
const openAppRoute = (route) => {
// Summary:
// Helper function for developers to navigation
// to a different route programmatically.
hashHistory.push(route);
};
window.openAppRoute = openAppRoute;
$("#loginBtn").click(
function(){
var email = $('loginEmail').val();
var pass = $('loginpwd').val();
/* Check to see if credential are correct.
* If so, change to '/home route
*/
window.openAppRoute("/home");
}
);
关于javascript - React-Router 使用 jQuery 浏览路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40667369/