Es6格式错误?

时间:2017-04-16 08:01:50

标签: javascript html jsp ecmascript-6

此字符串变量在js中定义。

bodycontent = 'Hello ' + studentName + '%0D%0AThe course ' + courseName
                + ' is using the TEAMMATES System to collect feedback.%0D%'
                + '0ATo \'join\' the course, please go to this Web address: '
                + encodeURIComponent(uri) + '%0D%0A*If prompted to log in, '
                + 'use your Googleaccount to log in. If you do not '
                + 'have a Google account, please create one from the '
                + encodeURIComponent('https://accounts.google.com/NewAccount')
                + '%0D%0A*The above link is unique to you. Please do not '
                + 'share it with your classmates.%0D%0ANote that If you '
                + 'wish to access TEAMMATES without using your Googleaccount, '
                + 'you do not need to \'join\' the course as instructed '
                + 'above. You will still be able to submit/view feedback '
                + 'by following the instructions sent to you by TEAMMATES at the '
                + 'appropriate times. However, we recommend joining the courseusing '
                + 'your Google account, because it gives you more convenient '
                + 'access to all your feedback stored in TEAMMATES.%0D%0A%0D%0A'
                + 'If you encounter any problems when using the system, you can '
                + 'email TEAMMATES support team at teammates@comp.nus.edu.sg.%0D%0A%0D%0A'
                + 'Regards,%0D%0ATEAMMATES Team.';

如果一行超过125个字符,我现在使用+将其分解为下一行,问题是当我将其转换为ES6时。

        bodycontent = `Hello ${studentName}%0D%0AThe following feedback session is ${status}%0D%0A`
                   + `Course: [${Id}]${courseName}%0D%0AFeedback Session Name: ${Id}%0D%0A`
                   + `The link of the feedback for the above session, please go to this Web `
                   + `address: ${encodeURIComponent(uri)}%0D%0A*The above link is unique to you.`
                   + `Please do not share it with others.%0D%0A%0D%0AIf you encounter any problems`
                   + `when using the system, you can email TEAMMATES support team at teammates@comp.nus.edu.sg.%0D%0A%0D%0ARegards,%0D%0ATEAMMATES Team.`;

这显示我错误“字符串必须使用单引号”在每行不包含变量。

我也可以像这样打破这条线

bodycontent = `Hello ${studentName}%0D%0AThe following feedback session is ${status}%0D%0A
                    Course: [${Id}]${courseName}%0D%0AFeedback Session Name: ${Id}%0D%0A
                    The link of the feedback for the above session, please go to this Web 
                   address: ${encodeURIComponent(uri)}%0D%0A*The above link is unique to you.`
                   ......

但问题是我在我的字符串中添加“\ n”添加换行符,我不希望告诉我在不添加字符串中的\ n的情况下打破该行的有效方法。

2 个答案:

答案 0 :(得分:1)

模板文字中包含的任何空格也将包含在内,这就是您收到换行符的原因。 这是模板文字的预期行为。模板文字的功能允许它用于在多行上创建字符串,而无需解决方法,例如:

$('div.post').html("<h3>Some title here</h3> " +
  "<p>Lorem ipsum doloramet, onsectetur adipiscing elit " +
  "sed do eiusmod tempor incididunt ut labore...");

变为:

$('div.post').html(`
  <h3>Some title here</h3>
  <p>Lorem ipsum doloramet, onsectetur adipiscing elit
  sed do eiusmod tempor incididunt ut labore...
`);

由于字符串的长度,您似乎只想运行多行,因此此问题与主观文本编辑首选项有关。< / p>

  

建议的意见:

     

您是否尝试在IDE中使用 文本换行

答案 1 :(得分:0)

使用带有common-tags包的令人敬畏的前缀模板文字。如果您不使用npm,也可以复制实施。

oneLine标记会将所有换行符和缩进更改为一个空格。如果您想要删除最后一个空格,请使用oneLineTrim