使用模板文字在 javascript 中构建多行字符串

Building a multi-line string in javascript with template literals

我正在尝试使用模板文本构建一个包含多个查询参数的 URL。

为了便于阅读,我将每个查询参数放在单独的一行中。看起来像下面的例子,只是长了很多。

const url = `http://example.com/hey?
one=${one}&
two=${two}&
three=${three}`;

我的问题涉及制作一个多行文字字符串,该字符串的最终值没有每个参数之间的换行符 (\n) 字符。模板文字是否可行,或者我应该以旧方式连接字符串?

您可以在模板文字的行尾使用反斜杠,多行,但没有换行符。

const 
    one = 'eins', two = 'zwei', three = 'drei',
    url = `http://example.com/hey?\
one=${one}&\
two=${two}&\
three=${three}`;

console.log(url);