admin管理员组文章数量:1400294
I would like to show "I read and agree to the privacy policy.privacy policy." text and the 'privacy policy' part of it must be a link.
I tried below but output is "I read and agree to the [object object]."
const privacyLink = <a href="">Privacy Policy</a>;
{`I read and agree to the ${privacyLink}`}
How can I do this using string template?
I would like to show "I read and agree to the privacy policy.privacy policy." text and the 'privacy policy' part of it must be a link.
I tried below but output is "I read and agree to the [object object]."
const privacyLink = <a href="">Privacy Policy</a>;
{`I read and agree to the ${privacyLink}`}
How can I do this using string template?
Share Improve this question edited Jul 17, 2017 at 6:29 Negin Basiri asked Jul 17, 2017 at 5:55 Negin BasiriNegin Basiri 1,3754 gold badges26 silver badges49 bronze badges 4-
1
You seem to be missing quotes around the
privacyLink
string. – castletheperson Commented Jul 17, 2017 at 5:59 - …and where are the braces around the template literal ing from? – Bergi Commented Jul 17, 2017 at 5:59
- 2 If this is about react/jsx, it's not ES6. – Bergi Commented Jul 17, 2017 at 6:00
- @4castle can you please write the correct code? – Negin Basiri Commented Jul 17, 2017 at 6:14
2 Answers
Reset to default 4privacyLink
is missing quotes. The template literal syntax is not correct. Define privacyLink
as a string, define a separate template literal including privacyLink
.
const privacyLink = "<a href=>Privacy Policy</a>";
const template = `I read and agree to the ${privacyLink}`;
document.body.innerHTML += template;
or simply define privacyLink
as a string and use +=
operator and .innerHTML
to set the .innerHTML
of an element within document
const privacyLink = "I read and agree to the <a href=>Privacy Policy</a>";
document.body.innerHTML += privacyLink;
Your question is missing the JSX tag, but you're clearly using JSX syntax so I'll answer it based on that interpretation.
When you write JSX, you're actually telling your transpiler to create a function, e.g. if you're using JSX with React, then
<a href="">Privacy Policy</a>
is
React.createElement('a', { href: '' }, 'Privacy Policy');
And the createElement returns on object, which is what's showing in your string template.
You can use string templates for building strings, so anything that you include inside the ${}
must be a string, or it will be cast as a string.
You should continue to use JSX syntax for writing anything you want to include another JSX element. This can be a bit tricky, since any element tree needs to have a root element. Generally you're pretty safe to use a <span>
, so in this case:
<span>I read and agree to the {privacyLink}</span>
本文标签: javascriptText with a link using ES6 templateStack Overflow
版权声明:本文标题:javascript - Text with a link using ES6 template - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744243289a2596889.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论