javascript - 如何使用 React Router 将 div 放入 Switch 中?
问题描述
我有一个使用 React 和 Redux 的 APP,我想NotFound
在用户输入无效路由时加载一个组件。我在网上找到了解决这个问题的方法,就是把所有的路由都放在一个交换机里面,包括NotFound
组件。
问题是,在我的应用程序中,我不能将所有路由都放在 Switch 中,因为有一个组件需要拉伸到整个页面,而所有其他组件都需要放在“容器”中。我在下面的方式(参见代码),该NotFound
组件适用于所有情况,除非我在“着陆”组件路线上(NotFound
组件始终显示)。
我试图Switch
用“容器” div 将登陆组件放在里面,但应用程序崩溃了。
有没有办法解决这个问题?(将着陆组件放在容器外,其他组件放在容器内)
class App extends Component {
render() {
return (
<Provider store={store}>
<Router>
<div className="App">
<Navbar />
<Route exact path="/" component={Landing} />
<div className="container">
<Switch>
<Route exact path="/register" component={Register} />
<Route exact path="/login" component={Login} />
<Route exact path="/profiles" component={Profiles} />
<Route exact path="/profile/:handle" component={Profile} />
<PrivateRoute exact path="/dashboard" component={Dashboard} />
<PrivateRoute
exact
path="/create-profile"
component={CreateProfile}
/>
<PrivateRoute
exact
path="/edit-profile"
component={EditProfile}
/>
<PrivateRoute
exact
path="/add-experience"
component={AddExperience}
/>
<PrivateRoute
exact
path="/add-education"
component={AddEducation}
/>
<PrivateRoute
exact
path="/edit-education/:id"
component={EditEducation}
/>
<PrivateRoute exact path="/feed" component={Posts} />
<PrivateRoute exact path="/post/:id" component={Post} />
<Route component={NotFound}/>
</Switch>
</div>
<Footer />
</div>
</Router>
</Provider>
);
}
}
解决方案
您可以为除登录页面之外的所有其他组件制作单独的路由器。
// App.js
import NonLandingPages from './NonLandingPages';
class App extends Component {
render() {
return (
<Provider store={store}>
<Router>
<div className="App">
<Navbar />
<Switch>
<Route exact path="/" component={Landing} />
<Route component={NonLandingPages}/>
</Switch>
<Footer />
</div>
</Router>
</Provider>
);
}
}
所有其他页面的单独路由器
// NonLandingPages.js
class NonLandingPages extends Component {
render() {
return (
<div className="container">
<Switch>
<Route exact path="/register" component={Register} />
<Route exact path="/login" component={Login} />
<Route exact path="/profiles" component={Profiles} />
<Route exact path="/profile/:handle" component={Profile} />
<PrivateRoute exact path="/dashboard" component={Dashboard} />
<PrivateRoute
exact
path="/create-profile"
component={CreateProfile}
/>
<PrivateRoute
exact
path="/edit-profile"
component={EditProfile}
/>
<PrivateRoute
exact
path="/add-experience"
component={AddExperience}
/>
<PrivateRoute
exact
path="/add-education"
component={AddEducation}
/>
<PrivateRoute
exact
path="/edit-education/:id"
component={EditEducation}
/>
<PrivateRoute exact path="/feed" component={Posts} />
<PrivateRoute exact path="/post/:id" component={Post} />
<Route component={NotFound}/>
</Switch>
</div>
);
}
}
推荐阅读
- time-series - Superset:如何改变“时间颗粒”
- python-3.x - 将ASCII转换为十进制python
- git - Git如何在分支之间传输更改?
- timer - 中断与任务、定时器中断、任务的关系
- google-sheets - 整个列的 Google 表格条件格式
- sql-server - 存在的机制
- python - 根据另一个具有不同大小python的列表替换nan的列表
- node.js - 从 API 正确返回 x-auth 令牌
- postgresql - 气流 psycopg2.OperationalError: FATAL: 抱歉,已经有太多客户
- javascript - 静态网站翻译问题