2017-04-16 83 views
1

這個字符串變量在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。

回答

0

使用帶有common-tags包的真棒前綴模板文字。如果您不使用npm,您也可以複製實現。

oneLine標記會將所有換行符和縮進更改爲一個空格。如果您想剝離最後一個空格,請使用oneLineTrim

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

好像你只想因爲字符串的長度而跑多行,所以這個問題是關於你的主觀文本編輯偏好

建議Soution:

您是否嘗試過使用文本換行在IDE?