将长模板文字行换成多行,而不在字符串中创建新行 [英] Wrap long template literal line to multiline without creating a new line in the string

查看:155
本文介绍了将长模板文字行换成多行,而不在字符串中创建新行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



在es6模板文字中,如何将长模板文字包装到多行而不在字符串中创建新行?例如,如果您这样做:

  const text =`一个很长的字符串,只是继续
并继续并继续`

然后,它将为字符串创建一个新的行符号,将其解释为具有一个新行。如何将长模板文字包装到多行而不创建换行?

解决方案

如果您引入行延续 \ )在文字的换行符点,它不会在输出中创建换行符:

  const text =`一个非常长的字符串,只是继续\ 
并继续并继续;
console.log(text); //一个很长的字符串,只是继续并继续并继续


In es6 template literals, how can one wrap a long template literal to multiline without creating a new line in the string?

For example, if you do this:

const text = `a very long string that just continues
and continues and continues`

Then it will create a new line symbol to the string, as interpreting it to have a new line. How can one wrap the long template literal to multiple lines without creating the newline?

解决方案

If you introduce a line continuation (\) at the point of the newline in the literal, it won't create a newline on output:

const text = `a very long string that just continues\
and continues and continues`;
console.log(text); // a very long string that just continuesand continues and continues

这篇关于将长模板文字行换成多行,而不在字符串中创建新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆