简体   繁体   English

如何使用ES6字符串模板添加多个空格?

[英]How do I add multiple spaces with ES6 string templates?

I have a string template that currently looks like this: 我有一个字符串模板,目前看起来像这样:

var option = "\u00A0" + "\u00A0" + "\u00A0" + "\u00A0" + option.name;

that I am trying to change to the new ES6 syntax 我正在尝试更改为新的ES6语法

var option = `    ${option.name}`

But when it shows up in on screen none of the spaces are in the ES6 version or there is no 4 space indention on the strings in which I am specifying it. 但是当它出现在屏幕上时,ES6版本中没有任何空格,或者我指定它的字符串上没有4个空格缩进。 The problem might have something to do with me using these strings in as options in a select . 这个问题可能与我在select使用这些字符串作为options有关。 Any ideas? 有任何想法吗?

In the first example you are using a non-breaking space ( ) and in the second example a normal space ( \ ). 在第一个示例中,您使用的是不间断空格( ),在第二个示例中使用的是普通空格( \ )。 So in addition to changing the syntax, you also changed the characters. 因此,除了更改语法之外,您还更改了字符。

This doesn't really have anything to do with ES6 in particular. 这与ES6没有任何关系。 If you use the same character it will work as expected: 如果您使用相同的字符,它将按预期工作:

var option = `\u00A0\u00A0\u00A0\u00A0${option.name}`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM