React Router仅识别索引路由 [英] React Router only recognises index route

查看:43
本文介绍了React Router仅识别索引路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2条路线,//about ,并且我已经测试了另外几条路线.所有路由仅呈现/的home组件.

I have 2 routes, / and /about and i've tested with several more. All routes only render the home component which is /.

当我尝试一条不存在的路线时,它会识别出正确的路线并显示警告警告:没有路由与路径"/example"匹配.确保您具有< Route path ="/example">您路线中的某处

When I try a route that doesn't exist it recognises that fine and displays the warning 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" path ="/about" handler = {About}/>

I've tried adding an explicit path to about to no avail. <Route name="about" path="/about" handler={About} />

我偶然发现了这个 stackoverflow Q ,但没有找到救赎的答案.

I've stumbled upon this stackoverflow Q but found no salvation in its answer.

任何人都可以弄清楚可能是什么问题吗?

Can anyone shed some light on what might be the problem?

推荐答案

由于您已将 About 嵌套在 Home 下,因此需要呈现< RouteHandler/code>主页组件中的/> 组件,以使React Router能够显示您的路由组件.

Since you've nested About under Home you need to render a <RouteHandler /> component within your Home component in order for React Router to be able to display your route components.

import {RouteHandler} from 'react-router';

var Home = React.createClass({
    render() {
        return (<div> this is the main component
            <RouteHandler />
        </div>);
    }
});

这篇关于React Router仅识别索引路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆