简体   繁体   English

如何将{variable}连接到react js中的href标签?

[英]How to concatenate {variable} to href tag in react js?

I am trying to use variable in curly braces in anchor tag's href string.我正在尝试在锚标记的 href 字符串中使用花括号中的变量。 suppose value of {entry.email} is abc@xyz.com then I am trying to do this <a href="mailto:"+{entry.email}>{entry.email}</a> to generate anchor tag <a href="mailto:abc@xyz">abc@xyz假设 {entry.email} 的值为 abc@xyz.com 那么我正在尝试这样做<a href="mailto:"+{entry.email}>{entry.email}</a>以生成锚标记<a href="mailto:abc@xyz">abc@xyz

in following code在以下代码中

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

But clearly this is not working or even compiling.但显然这不起作用,甚至无法编译。 How do I do this?我该怎么做呢?

Try with template literals尝试使用模板文字

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

or或者

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

You can use this:你可以使用这个:

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

String concatenation whether using quote marks or string literals will work as long as they are declared inside curly braces...字符串连接无论使用引号还是字符串文字,只要它们在花括号内声明就可以工作......

OPTION ONE:选项一:

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

OPTION TWO:选项二:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM