我需要在句子之间添加中断标签,但是当我这样做时,我会得到 [object Object] 来代替换行符。有人可以告诉我如何解决这个问题吗?
const openingText = 'We\'re happy to hear your project is underway. '
+ <br /> + 'You can review this business to tell others about your experience.';
浏览器中的示例:
开发工具中的示例:
当我这样写时,出现语法错误:
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 标签时,它只是认为它是常规文本:
const openingText = 'We\'re happy to hear your project is underway. <br /><br />' +
'You can review this business to tell others about your experience.';
最佳答案
因为您无法添加字符串
和对象
:
var tmp = {}
console.log('This is an object: ' + tmp)
你想要用 React 做的是:
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 short syntax 的工具。 ,在这种情况下您应该能够执行以下操作:
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>
);
关于javascript - 当我的 javascript 呈现时,为什么我的 <br/> 标记显示为 [object Object]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54934604/