为什么我的 <br/>当我的 javascript 被渲染时,标签显示为 [object Object]? [英] Why does my <br /> tag show as an [object Object] when my javascript is rendered?
问题描述
我需要在句子之间添加中断标记,但是当我这样做时,我得到 [object Object] 代替换行符.有人可以告诉我如何解决这个问题吗?
I need to add break tags in between my sentences but when I do that I get [object Object] in place of a line break. Can someone please tell me how to fix this?
const openingText = 'We\'re happy to hear your project is underway. '
+ <br /> + 'You can review this business to tell others about your experience.';
浏览器中的示例:
开发工具中的示例:
当我这样写时,出现语法错误:
When I write it like this I get a syntax error:
const openingText = (
<>
We're happy to hear your project is underway.
<br /><br />
You can review this business to tell others about your experience.
</>
);
当我在引号内添加 br 标签时,它只是认为它是常规文本:
When I add the br tags inside of the quotes it just thinks it's regular text:
const openingText = 'We\'re happy to hear your project is underway. <br /><br />' +
'You can review this business to tell others about your experience.';
推荐答案
因为你不能添加 string
和 Object
:
Because you can't add a string
and an Object
:
var tmp = {}
console.log('This is an object: ' + tmp)
你想用 React 做什么:
What you want to do with React is:
const openingText = (
<>
We're happy to hear your project is underway.
<br />
You can review this business to tell others about your experience.
</>
);
如果您在使用该语法时遇到错误,则您可能使用的是旧版 react 或使用的工具不支持 React Fragment 简短语法,在这种情况下,您应该能够:
If you get an error using that syntax, you're probably on an older version of react or using a tool that doesn't support React Fragment short syntax, in which case you should be able to do:
const openingText = (
<React.Fragment>
We're happy to hear your project is underway.
<br />
You can review this business to tell others about your experience.
</React.Fragment>
);
这篇关于为什么我的 <br/>当我的 javascript 被渲染时,标签显示为 [object Object]?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!