Es6格式错误?

问题描述:

这个字符串变量在js中定义。Es6格式错误?

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 [email protected]%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 [email protected]%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。

使用带有common-tags包的真棒前缀模板文字。如果您不使用npm,您也可以复制实现。

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

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

$('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... 
`); 

好像你只想因为字符串的长度而跑多行,所以这个问题是关于你的主观文本编辑偏好

建议Soution:

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