javascript - 如何在同一域中运行两个 React JS 应用程序

标签 javascript url reactjs routes react-router

我有一个现有的 React 项目托管在某个域中,例如 http://xyz.dev 现在我想在同一个域中托管另一个 react 应用程序,但在不同的目录中,比如 xyz.dev/newApp。我面临的问题是 xyz.dev 的 react-router 将/newApp 视为其虚拟路由,并且它不会重定向到 newApp 而不是获取 xyz.dev 的内容。 有什么方法可以告诉 react-router 忽略所有到达/newApp 的请求。

<Route path="/" component={BootstrapApp} >
    <IndexRoute component={HomeApp} />
    <Route path="about" component={AboutusApp} />
    /* I need something like <Route ignorePath="newApp"/> */
</Route>

或者还有其他方法吗?我们会立即提供帮助。

最佳答案

终于明白了。它与一个叫做 Alias 的概念有关。我们需要在服务器中创建一个别名配置,以告诉服务器从不同的目录中获取/newApp 的所有内容。

Alias "/newApp" "C:/xampp/htdocs/react/xyz/newApp/www/public/"
<Directory "C:/xampp/htdocs/react/xyz/newApp/www/public/">
    Options -Indexes +FollowSymLinks +MultiViews
    AllowOverride None
    Require all granted
    RewriteEngine On
    RewriteBase /newApp
    RewriteCond %{REQUEST_FILENAME} -s [OR]
    RewriteCond %{REQUEST_FILENAME} -l [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^.*$ - [NC,L]
    RewriteRule ^.*$ index.html [NC,L]
</Directory>

同样在您的 newApp 路由中,我们必须将路由设置为

<Route path="/newApp/" component={BootstrapApp} >
    <IndexRoute component={HomeApp} />
    <Route path="about" component={AboutusApp} />
</Route>

仅使用这两种配置,我们就可以在同一域中运行两个完全不同的 React 应用程序。

关于javascript - 如何在同一域中运行两个 React JS 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39491834/

相关文章:

javascript - 在 Zotero(即 Firefox XPCOM/XUL)扩展中附加 SQLITE 数据库

javascript - 在 URL 中输入值

reactjs - 如何破坏.map函数

javascript - 在另一个 Saga 中使用 Action/Saga

JavaScript + SVG : get fill when it has been declared outside of a style

javascript - 如何在 Rails 中调用 javascript 后渲染?

javascript - 如何在页面上创建漂亮流畅的滚动效果?

javascript - 内部问号或 angularjs 的 url 出现问题

javascript - 根据页面子域本地化脚本 URL

javascript - 将路径参数传递给 React 上下文提供者