简体   繁体   中英

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

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. The problem might have something to do with me using these strings in as options in a select . 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. If you use the same character it will work as expected:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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