首页 > 解决方案 > 是否可以使用 react-router-dom 动态导航不同的本地主机?

问题描述

我有一个 Web 应用程序,目前我正在通过localhost:3002. 在此 Web 应用程序中,我使用来自此卡的卡片,reactstrap并且在此卡片上有一个我要从中导入的链接 我import { Link } from 'react-router-dom'; 正在使用此链接转到应用程序的特定详细页面。

问题:我同时运行另一个应用程序,localhost:5000它是一个登录系统。从Link我描述的卡片中,我可以使用“项目规格”下方的按钮转到其他本地主机为 5000 的应用程序吗?

眼镜

在代码片段下方:

import React from 'react';
import { Card, CardTitle, CardSubtitle, CardText, CardBody, CardImg } from 'reactstrap';
import { Link } from 'react-router-dom';

<CardTitle>
    <h3 className="thick">{ship.name}</h3>
</CardTitle>

<CardText>
    <h6>Project Details</h6>
    <p>For a description of the project view the specification included</p>
</CardText>
<div class="btn-toolbar">
    <Link to="/localhost:5000/login" className="btn btn-primary mr-3">
    Go to Specs
    </Link>
    <Link to={`/vessels/${ship.slug}`} className="btn btn-primary">
Go to vessels
</Link>
</div>

到目前为止我做了什么:

1)从这篇文章中,我能够正确设置react-router-dom并且实际上工作正常。

2)我试图将它链接到另一个localhost:5000,因为当我将它链接到我的应用程序上已经存在的特定页面时,只需键入即可<Link to="/localhost:5000/" className="btn btn-primary mr-3">,不幸的是它不起作用。

3)我也遇到了这个问题,这对于理解如何浏览页面而不是通过不同的本地主机很有用

标签: javascriptreactjsreact-router-dom

解决方案


react-router-dom 的 Link 组件旨在链接到应用程序内的路由。要外部链接,您可以使用 HTML 'a' 标签: <a href="http://localhost:5000" className="btn btn-primary mr-3">

更多信息和其他可能的解决方法:

https://github.com/ReactTraining/react-router/issues/1147

使用 react-router-dom 重定向到第三方 url


推荐阅读