React Router 只识别索引路由
React Router only recognises index route
我有 2 条路线,/
和 /about
,我还测试了更多路线。所有路由只渲染 /
的 home 组件。
当我尝试一条不存在的路线时,它认为很好并显示警告
Warning: No route matches path "/example". Make sure you have <Route path="/example"> somewhere in your routes
App.js
import React from 'react';
import Router from 'react-router';
import { DefaultRoute, Link, Route, RouteHandler } from 'react-router';
import {Home, About} from './components/Main';
let routes = (
<Route name="home" path="/" handler={Home} >
<Route name="about" handler={About} />
</Route>
);
Router.run(routes, function (Handler) {
React.render(<Handler/>, document.body);
});
./components/Main
import React from 'react';
var Home = React.createClass({
render() {
return <div> this is the main component </div>
}
});
var About = React.createClass({
render(){
return <div>This is the about</div>
}
});
export default {
Home,About
};
我试过添加一个显式路径到 about 无济于事。
<Route name="about" path="/about" handler={About} />
我偶然发现了这个Whosebug Q,但在它的答案中找不到任何救赎。
任何人都可以阐明可能是什么问题吗?
由于您在 Home
下嵌套了 About
,您需要在 Home
组件中渲染一个 <RouteHandler />
组件,以便 React Router 能够显示你的路线组成部分。
import {RouteHandler} from 'react-router';
var Home = React.createClass({
render() {
return (<div> this is the main component
<RouteHandler />
</div>);
}
});
使用 ES6 和最新的 react-router 看起来像这样:
import React from 'react';
import {
Router,
Route,
IndexRoute,
}
from 'react-router';
const routes = (
<Router>
<Route component={Home} path="/">
<IndexRoute component={About}/>
</Route>
</Router>
);
const Home = React.createClass({
render() {
return (
<div> this is the main component
{this.props.children}
</div>
);
}
});
//Remember to have your about component either imported or
//defined somewhere
React.render(routes, document.body);
附带说明一下,如果您想将未找到的路由与特定视图匹配,请使用:
<Route component={NotFound} path="*"></Route>
注意路径设置为 *
也编写自己的 NotFound 组件。
我的看起来像这样:
const NotFound = React.createClass({
render(){
let _location = window.location.href;
return(
<div className="notfound-card">
<div className="content">
<a className="header">404 Invalid URL</a >
</div>
<hr></hr>
<div className="description">
<p>
You have reached:
</p>
<p className="location">
{_location}
</p>
</div>
</div>
);
}
});
我有 2 条路线,/
和 /about
,我还测试了更多路线。所有路由只渲染 /
的 home 组件。
当我尝试一条不存在的路线时,它认为很好并显示警告
Warning: No route matches path "/example". Make sure you have <Route path="/example"> somewhere in your routes
App.js
import React from 'react';
import Router from 'react-router';
import { DefaultRoute, Link, Route, RouteHandler } from 'react-router';
import {Home, About} from './components/Main';
let routes = (
<Route name="home" path="/" handler={Home} >
<Route name="about" handler={About} />
</Route>
);
Router.run(routes, function (Handler) {
React.render(<Handler/>, document.body);
});
./components/Main
import React from 'react';
var Home = React.createClass({
render() {
return <div> this is the main component </div>
}
});
var About = React.createClass({
render(){
return <div>This is the about</div>
}
});
export default {
Home,About
};
我试过添加一个显式路径到 about 无济于事。
<Route name="about" path="/about" handler={About} />
我偶然发现了这个Whosebug Q,但在它的答案中找不到任何救赎。
任何人都可以阐明可能是什么问题吗?
由于您在 Home
下嵌套了 About
,您需要在 Home
组件中渲染一个 <RouteHandler />
组件,以便 React Router 能够显示你的路线组成部分。
import {RouteHandler} from 'react-router';
var Home = React.createClass({
render() {
return (<div> this is the main component
<RouteHandler />
</div>);
}
});
使用 ES6 和最新的 react-router 看起来像这样:
import React from 'react';
import {
Router,
Route,
IndexRoute,
}
from 'react-router';
const routes = (
<Router>
<Route component={Home} path="/">
<IndexRoute component={About}/>
</Route>
</Router>
);
const Home = React.createClass({
render() {
return (
<div> this is the main component
{this.props.children}
</div>
);
}
});
//Remember to have your about component either imported or
//defined somewhere
React.render(routes, document.body);
附带说明一下,如果您想将未找到的路由与特定视图匹配,请使用:
<Route component={NotFound} path="*"></Route>
注意路径设置为 *
也编写自己的 NotFound 组件。
我的看起来像这样:
const NotFound = React.createClass({
render(){
let _location = window.location.href;
return(
<div className="notfound-card">
<div className="content">
<a className="header">404 Invalid URL</a >
</div>
<hr></hr>
<div className="description">
<p>
You have reached:
</p>
<p className="location">
{_location}
</p>
</div>
</div>
);
}
});