React-router-dom v4 嵌套路由不起作用
React-router-dom v4 nested routes not working
参考未解决的问题(作为最终结论)
我也遇到了同样的问题。
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)
老实说,我不太确定你在问什么。我假设您正在尝试让 "Something not working" 示例正常工作。
像这样,
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>
)
这是执行您所描述的操作的一种方法(请注意,还有其他方法可以直接在您的 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;
这将执行以下操作,希望这就是您现在在问题中描述的内容:
/login
没有页眉或页脚。
/
、/list
和 /settings
都有页眉和页脚。
- 任何未找到的路由都将显示
PageNotFound
组件,没有页眉或页脚。
参考未解决的问题(作为最终结论)
我也遇到了同样的问题。
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)
老实说,我不太确定你在问什么。我假设您正在尝试让 "Something not working" 示例正常工作。
像这样,
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>
)
这是执行您所描述的操作的一种方法(请注意,还有其他方法可以直接在您的 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;
这将执行以下操作,希望这就是您现在在问题中描述的内容:
/login
没有页眉或页脚。/
、/list
和/settings
都有页眉和页脚。- 任何未找到的路由都将显示
PageNotFound
组件,没有页眉或页脚。