将长模板文字行包装为多行而不在字符串中创建新行

新手上路,请多包涵

在 es6 模板文字中,如何在不在字符串中创建新行的情况下将长模板文字包装成多行?

例如,如果您这样做:

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

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

原文由 Ville Miekk-oja 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 232
2 个回答

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

 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

原文由 CodingIntrigue 发布,翻译遵循 CC BY-SA 3.0 许可协议

这是一个旧的。但它出现了。如果您在编辑器中留下任何空格,它将把它们放在那里。

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

只是做正常的 + 符号

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

原文由 Monte Jones 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题