我使用的是打字稿,并且在连接字符串时抱怨
const content = senderDisplay + ', '+ moment(timestamp).format('YY/MM/DD')+' at ' + moment(timestamp).format('h:mm A');
[tslint]使用模板文字而不是与字符串连接 文字。 (比较喜欢模板)
修复此问题的模板文字是什么?欢呼声
答案 0 :(得分:10)
您可以看到template literals in MDN,这些是ES6中的首选风格。
在您的情况下,它将成为以下内容:
const content = `${senderDisplay}, ${moment(timestamp).format('YY/MM/DD')} at ${moment(timestamp).format('h:mm A')}`;
重要差异:
以反引号开始和结束
支持多行字符串
使用${expression}
答案 1 :(得分:4)
const content = `${senderDisplay}, ${moment(timestamp).format('YY/MM/DD')} at ${moment(timestamp).format('h:mm A')};`
查看https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals!一旦掌握了它,它们就会很棒,字符串插值比连接它们更具可读性。
答案 2 :(得分:3)
使用反引号和${...}
。
const content = `${senderDisplay}, ${moment(timestamp).format('YY/MM/DD')} at ${moment(timestamp).format('h:mm A')}`;