【发布时间】:2020-09-13 20:10:58
【问题描述】:
我正在尝试使用 react-router v5 为 React 构建多用途网站进行路由。 对于普通路由,我们有类似的路由
本地主机:3000/帖子 本地主机:3000/画廊
但对于其他一些情况,我想拥有子域让我们说博客。
blog.localhost:3000 // 渲染博客组件
或
subdomain.localhost:3000 // 渲染一些组件。
现在这也渲染与 localhost:3000 相同的组件。
那么,我有什么办法可以在反应中做到这一点吗?
当前路线如下所示。
function App() {
return (
<React.Fragment>
<React.Suspense fallback={<></>}>
<Router history={history}>
<Switch>
<Redirect exact from="/" to="/home" />
<Route exact path="/login" component={LoginPage} />
<ProtectedRoute exact={true} path="/profile" component={ProfilePage} />
<ProtectedRoute exact={true} path="/home" component={HomePage} />
<ProtectedRoute exact={true} path="/gallery/:subdomain" component={Gallery} />
<Route exact path="/gallery/post/:subdomain/:postid" component={SinglePostView} />
<Route component={PageNotFound} />
</Switch>
</Router>
</React.Suspense>
</React.Fragment>
);
}
【问题讨论】:
-
无法将子域添加到 localhost,但请参阅此问题:stackoverflow.com/questions/19016553/…
-
嗨@zirmax 感谢您的回复,这是主机配置的一部分,但我想知道如果没有反应路由器中的配置,我们是否可以做到这一点。如果不可能,为什么子域会呈现与没有子域相同的组件?
标签: javascript reactjs routes react-router