[英]React Router Navigation
Learning React here and trying to get routes to work. 在这里学习React并尝试找到可行的途径。 This is an extremely minimal thing just to get a feel for the syntax and etc. The code below works as I enter the route one and two in the address bar. 这只是为了使语法和其他方面的感受变得极其简单。当我在地址栏中输入路由一和二时,下面的代码起作用。 But I wanted to build a navigation bar. 但是我想建立一个导航栏。 Assuming I could simply add HTML in App.js to accomplish this, I was made aware that using <a href=
is not the way to go and use Link instead. 假设我可以简单地在App.js中添加HTML来完成此操作,我意识到使用<a href=
并不是使用链接的方法。 That works fine with the <Link>
tags inside my routes but say I wanted to add a nav to App.js, using the same Link method does not work. 这可以与我的路线内的<Link>
标记一起使用,但是我想使用相同的Link方法将导航添加至App.js。
I tried adding: 我尝试添加:
<h1>
<Link to="/route-one">One</Link>
|
<Link to="/route-two">Two</Link>
</h1>
<hr />
<Router>
...
But 'One' and 'Two' are not links. 但是“一个”和“两个”不是链接。 Though in the browser console I am seeing: 虽然在浏览器控制台中,我看到了:
TypeError: this.context.history is undefined
I asked the instructor but he did not want to help. 我问老师,但他不想帮忙。
See the code I currently have below; 请参阅下面的代码。
This is the App.js file: 这是App.js文件:
import React from 'react';
import ReactDOM from 'react-dom';
import { Router, Route, IndexRoute } from 'react-router';
import RouteOne from './RouteOne.js';
import RouteTwo from './RouteTwo.js';
import { Link } from 'react-router';
const App = React.createClass({
render() {
return (
<div>
<Router>
<Route path="route-one" component={RouteOne} />
<Route path="route-two" component={RouteTwo} />
</Router>
</div>
)
}
});
ReactDOM.render(<App />, document.getElementById("app"));
This is the RouteOne.js: 这是RouteOne.js:
import React from 'react';
import { Link } from 'react-router';
var RouteOne = React.createClass({
render() {
return (
<div>
<h2>Route One</h2>
<Link to="/route-two">
<h3>Switch Route</h3>
</Link>
</div>
)
}
});
export default RouteOne;
This is RouteTwo.js: 这是RouteTwo.js:
import React from 'react';
import { Link } from 'react-router';
var RouteTwo = React.createClass({
render() {
return (
<div>
<h2>Route Two</h2>
<Link to="/route-one">
<h3>Switch Route</h3>
</Link>
</div>
)
}
});
export default RouteTwo;
Not sure if this is the "ideal" way but having been looking at an example, I was entering the link path incorrectly. 不确定这是否是“理想”方式,但在查看示例后,我输入的链接路径错误。
This: 这个:
<Link to="/#/route-one">One</a>
<Link to="/#/route-two">Two</a>
worked as it should instead of: 按照应有的方式工作,而不是:
<Link to="/route-one">One</Link>
<Link to="/route-two">Two</Link>
From the react-router documentation: 从react-router文档中:
https://github.com/rackt/react-router/blob/latest/docs/guides/basics/Histories.md https://github.com/rackt/react-router/blob/latest/docs/guides/basics/Histories.md
React Router is built with
history
. React Router是用history
构建的。 In a nutshell, a history knows how to listen to the browser's address bar for changes and parses the URL into a location object that the router can use to match routes and render the correct set of components. 简而言之,历史记录知道如何侦听浏览器的地址栏以进行更改,并将URL解析为一个位置对象,路由器可以使用该对象来匹配路由并呈现正确的组件集。
Add one of the default implementation in your import
line: 在import
行中添加默认实现之一:
import { Router, Route, IndexRoute, browserHistory } from 'react-router';
Add the route prop
on your root Router
definition: 在您的根Router
定义上添加路由prop
:
<Router history={browserHistory}>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.