首页 > 解决方案 > React 中的子域路由

问题描述

我正在尝试使用 react-router v5 构建多用途网站以进行路由。对于普通路由,我们有类似的路由

本地主机: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>
  );
}

标签: javascriptreactjsroutesreact-router

解决方案


这是不可能的,React 不是 Web 服务器,React 是单页应用程序。您必须在该子域上托管另一个 React 应用程序。


推荐阅读