如何在 ReactJS 中将 {variable} 连接到 href 标签中?

3

我想在锚点标签的href字符串中使用花括号变量。 假设{entry.email}的值为abc@xyz.com,那么我正在尝试这样做:<a href="mailto:"+{entry.email}>{entry.email}</a> 以生成锚点标签 <a href="mailto:abc@xyz">abc@xyz

在下面的代码中:

const rows = props.contacts.map((entry, index) => {
    return (
        <tr key={index}>
            <td><a href="mailto:"+{entry.email}>{entry.email}</a></td>
        </tr>
    );

但显然这并没有起作用,甚至没有编译。我该如何做到这一点?

3个回答

8

使用模板字面量尝试

href={`mailto: ${entry.email}`}

或者

href={"mailto: "+ entry.email}

0

字符串连接无论是使用引号还是字符串字面量,只要它们在花括号内声明即可正常工作...

选项一:

<a href={"mailto: "+ entry.email}>

选项二:

<a href={`mailto: ${entry.email}`}>

0
你可以使用这个:
<a href={`mailto:${entry.email}`}>

这与所接受的答案有何不同? - JustCarty

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接