reactjs 想在react js中从html字符串制作电子邮件模板

qco9c6ql  于 2023-04-11  发布在  React
关注(0)|答案(1)|浏览(134)
const person = {
    name: 'Wes',
    job: 'Web Developer',
    city: 'Hamilton',
    bio: 'Wes is a really cool guy that loves to teach web development!'
}

const emailData = '<div class="person"><h2>${person.name}</h2><p class="location">${person.city}</p><p class="bio">${person.bio}</p></div>'

我有一个对象,我想用emailData绑定该对象数据。
我的期望是:

<div class="person"><h2>Wes</h2><p class="location">Hamilton</p><p class="bio">Wes is a really cool guy that loves to teach web development!</p></div>
jexiocij

jexiocij1#

假设你想在React中得到答案,你描述的这个模式很容易转换成一个组件:

function EmailData(props) {
  return <div className="person">
    <h2>{props.name}</h2>
    <p className="location">{props.city}</p>
    <p className="bio">{props.bio}</p>
  </div>
}

然后,您可以将其称为:

<EmailData name="Wes" city="Hamilton" bio="..." />

或者,如果你已经有了person对象,你可以使用spread操作符:

<EmailData {...person} />

相关问题