React-router-dom v4嵌套路由不起作用

时间:2017-04-09 17:26:17

标签: reactjs react-router react-router-v4 react-router-dom

参考未解决的问题(作为最终结论)

我也遇到了同样的问题。

https://reacttraining.com/react-router/web/guides/quick-start宣传react-router-dom

此外,人们在一个文件中找到更好的list down routes而不是组件内部。

引用的内容: https://github.com/ReactTraining/react-router/tree/master/packages/react-router-config

工作(主要是):

import * as React from 'react'
import {BrowserRouter as Router, Route, Switch } from 'react-router-dom'


export const Routes = () => (
  <Router>
    <div>
      <Switch>
        <Route exact path="/login" component={Login}/>
        <MainApp path="/">
          <Route path="/list" component={List}/>
          <Route path="/settings" component={Settings}/>
        </MainApp>
        <Route path="*" component={PageNotFound}/>
      </Switch>
    </div>
  </Router>
)

有些东西不起作用 site.com/SomeGarbagePath显示我认为的<MainApp> <Route path="*" component={PageNotFound}/>

更新

/ - Home - parent of all (almost)
/List - inside home
/Settings - inside home
/Login - standalone
/Users - inside home, For now just showing itself. It has further pages.
/User/123 - inside user with /:id
/User/staticUser - inside user with static route
/garbage - not a route defined (not working as expected)

2 个答案:

答案 0 :(得分:9)

这是执行您所描述内容的一种方式(请注意,您可以直接在React组件中处理布局的其他方法)。为了使示例保持简单,其他组件(<Home>, <List>等)被创建为没有属性或状态的纯功能组件,但将每个组件作为正确的React组件放在其自己的文件中是微不足道的。以下示例已完成并将运行。

import React, { Component } from 'react';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';

class App extends Component {
  render() {

    const Header = () => <h1>My header</h1>;
    const Footer = () => <h2>My footer</h2>;
    const Login = () => <p>Login Component</p>;    
    const Home = () => <p>Home Page</p>;
    const List = () => <p>List Page</p>;
    const Settings = () => <p>Settings Page</p>;
    const PageNotFound = () => <h1>Uh oh, not found!</h1>;

    const RouteWithLayout = ({ component, ...rest }) => {
      return (
        <div>
          <Header />
          <Route {...rest} render={ () => React.createElement(component) } />
          <Footer />
        </div>
      );
    };

    return (
      <Router>
        <div>
          <Switch>
            <Route exact path="/login" component={Login} />
            <RouteWithLayout exact path="/" component={Home} />
            <RouteWithLayout path="/list" component={List} />
            <RouteWithLayout path="/settings" component={Settings} />
            <Route path="*" component={PageNotFound} />
          </Switch>
        </div>
      </Router>    
    );
  }
}

export default App;

这将执行以下操作,希望现在在您的问题中描述的内容:

  1. /login没有页眉或页脚。
  2. //list/settings都有页眉和页脚。
  3. 找不到的任何路由都会显示PageNotFound组件,没有页眉或页脚。

答案 1 :(得分:0)

我说实话,我不完全确定你在问什么。我假设你正试图让你的&#34;某些东西不起作用&#34;工作的例子。

像这样,

import * as React from 'react'
import {BrowserRouter as Router, Route, Switch } from 'react-router-dom'


export const Routes = () => (
  <Router>
    <div>
      <Switch>
        <Route exact path="/login" component={Login}/>
        <MainApp path="/">
          <Switch>
            <Route path="/list" component={List}/>
            <Route path="/settings" component={Settings}/>
          </Switch>
        </MainApp>
        <Route component={PageNotFound} /> 
      </Switch>
    </div>
  </Router>
)