我正在使用TypeScript,当连接字符串时出现错误提示,
const content = senderDisplay + ', '+ moment(timestamp).format('YY/MM/DD')+' at ' + moment(timestamp).format('h:mm A');
[tslint] 使用模板字面量而不是字符串连接。(prefer-template)
如何使用模板字面量来修复这个问题?谢谢。
我正在使用TypeScript,当连接字符串时出现错误提示,
const content = senderDisplay + ', '+ moment(timestamp).format('YY/MM/DD')+' at ' + moment(timestamp).format('h:mm A');
[tslint] 使用模板字面量而不是字符串连接。(prefer-template)
如何使用模板字面量来修复这个问题?谢谢。
const content = `${senderDisplay}, ${moment(timestamp).format('YY/MM/DD')} at ${moment(timestamp).format('h:mm A')}`;
重要区别:
以反引号开头和结尾
支持多行字符串
表达式使用${expression}
插值
const content = `${senderDisplay}, ${moment(timestamp).format('YY/MM/DD')} at ${moment(timestamp).format('h:mm A')};`
看看https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Template_literals吧!一旦你掌握了它,它们就非常棒。字符串插值比将字符串拼接在一起更易读。
使用反引号和${...}
。
const content = `${senderDisplay}, ${moment(timestamp).format('YY/MM/DD')} at ${moment(timestamp).format('h:mm A')}`;
prefer-template
这样的规则,以了解正在发生什么... - Jason Goemaat