我是新手。我有以下代码: 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
答案 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')