反应如何路由到外部链接

时间:2021-06-15 14:21:27

标签: reactjs

我是新手。我有以下代码: const CompanyCard = ({name, address, postcode, companyUrl}: company) => { const handleViewEmployees = (name: string) => { 控制台日志(名称); }

const openInNewTab = (url: string) => {
    const newWindow = window.open(url, '_blank', 'noopener,noreferrer')
    if (newWindow) newWindow.opener = null
}

return (
    <>
    <td>{name}</td>
    <td>{address}</td>
    <td>{postcode}</td>
    <td><a href="#" onClick={() => openInNewTab(companyUrl)}>{companyUrl}</a></td>
    <td><button onClick={() => handleViewEmployees(name)}>View Employees</button></td>
    </>
)

}

导出默认公司卡;

我想在新标签页中打开公司网址。 IE。网址“www.google.com”。目前它在 localhost:3000/www.google.com

之前打开

2 个答案:

答案 0 :(得分:0)

const openInNewTab = (url: string, shouldOpenNewTab: boolean = true) =>
shouldOpenNewTab ? window.open(url, "_blank") : window.location.href = url;

试试这个

答案 1 :(得分:0)

您应该将协议添加到 URL。因此,请在网址开头添加 https://http://

window.open(`https://${url}`, '_blank', 'noopener,noreferrer')