【问题标题】:React opens website link after localhost url instead of https://websitelinkReact 在 localhost url 之后打开网站链接,而不是 https://websitelink
【发布时间】:2021-08-31 15:32:44
【问题描述】:

我从数据库中获取雇主网址,当我单击网址文本时,它打开为 localhost:3000/webadresslink,如何将其更改为 https://webaddresslink

import React, { useState, useEffect } from 'react'
import EmployerService from '../../services/employerService'
import { Styles } from './style'
import { Card, Container } from 'react-bootstrap'

const EmployersList = () => {
  const [employers, setEmployers] = useState([])

  useEffect(() => {
    let employerService = new EmployerService()
    employerService.getAllEmployers().then(result => setEmployers(result.data.data))
  })
  return (
    <Styles>
      <Container>
        {
          employers.map(employer => (
            <Card>
              <Card.Body>
                <Card.Title>{employer.companyName}</Card.Title>
                <Card.Text>
                  <a href={employer.webAddress} target="blank">
                    {employer.webAddress}
                  </a>
                </Card.Text>
                <Card.Text>
                  {employer.email}
                </Card.Text>
                <Card.Text>
                  {employer.phoneNumber}
                </Card.Text>
              </Card.Body>
            </Card>
          ))
        }
      </Container>
    </Styles>
  )
}

【问题讨论】:

  • employer.webAddress 的实际运行时值是多少?
  • 以“kodlama.io”为例,点击时我的url:localhost:3000/kodlama.io

标签: reactjs react-link


【解决方案1】:

因为kodlama.io 不是绝对网址。它可能直观地在您看来很像,但它在结构上与其他相对 URL 没有任何不同,例如:

  • index.html
  • index.com
  • index.html.com
  • www.index.html

要告诉浏览器这是一个 不同主机 的 URL,您需要为其添加前缀:

  • https://kodlama.io

甚至简单地说:

  • //kodlama.io

如果显示的数据组织得不好,并且 some URL 值有前缀,而 some 没有,那么前端逻辑的可行尝试可能是简单地说:如果值不包含“//”,则在其前面加上“//”。例如:

href={
  employer.webAddress.includes('//') ?
  employer.webAddress :
  `//${employer.webAddress}`
}

【讨论】:

  • 非常感谢,感谢它成功了!
猜你喜欢
  • 2013-08-19
  • 2014-01-21
  • 2017-08-27
  • 2016-10-06
  • 2011-03-29
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 2019-01-31
相关资源
最近更新 更多