[英]es6 javascript recommend way to apply template literal with ternary operator?
I am using eslint in my development with airbnb style guide. 我在使用Airbnb样式指南进行开发时使用eslint。 I am getting eslint error suggest using template literals instead of string concatenation (prefer-template). 我收到错误提示,建议使用模板文字而不是字符串连接(首选模板)。
It flags this line with error 它标记此行有错误
':<br><br><pre style="color:red">' + softTab + err.stack.replace(/\\n/g, '<br>' + softTab) + '</pre>' : '';
here is my code 这是我的代码
const renderError = err => {
const softTab = '    ';
const errTrace = process.env.NODE_ENV !== 'production' ?
':<br><br><pre style="color:red">' + softTab + err.stack.replace(/\n/g, '<br>' + softTab) + '</pre>' : '';
return renderFullPage(`Server Error${errTrace}`, {});
};
My question is what is the recommend way to apply a template literal using ternary operator? 我的问题是使用三元运算符应用模板文字的推荐方法是什么? I applied it on the function return but depending on where I apply the template literal on this line causes errors. 我在函数返回上应用了它,但是取决于我在此行上应用模板文字的位置会导致错误。 Ideally I tried doing this whole line but I get a problem with with trying to pass ${softTab}
to the err.stack.replace(/\\n/g, '<br>' + softTab)
any suggestions or solutions would be great. 理想情况下,我尝试过整行,但是在尝试将${softTab}
传递给err.stack.replace(/\\n/g, '<br>' + softTab)
遇到问题,任何建议或解决方案都很好。 Thanks! 谢谢!
In ES6 you can use template literals instead of string concatenation. 在ES6中,可以使用模板文字而不是字符串连接。 To do this you use back-tick (`)(grave-accent) character instead of double or single quotes. 为此,请使用反引号(`)(grave-accent)字符而不是双引号或单引号。 The template literals can contain place holders. 模板文字可以包含占位符。 These are indicated by the dollar sign and curly braces (${expression}). 这些由美元符号和大括号($ {expression})表示。 So your case: 所以你的情况:
`:<br><br><pre style="color:red">${ softTab }${ err.stack.replace(/\n/g, `<br>${softTab}`) }</pre>`
For further reading on this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals 有关此内容的更多信息,请访问: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
This doesn't really have a lot to do with the ternary operator. 这实际上与三元运算符无关。 You should just replace the string concatentation with the template literal (or at least, that style guide says you should do that): 您应该只用模板文字替换字符串连接(或至少,该样式指南说您应该这样做):
`:<br><br><pre style="color:red">${ softTab }${ err.stack.replace(/\n/g, `<br>${softTab}`) }</pre>'
That said, you might want not to use a ternary at all here: 也就是说,您可能根本不想在这里使用三元数:
function renderError(err) {
const softTab = '    ';
let message = 'Server Error';
if (process.env.NODE_ENV !== 'production') {
message += ':<br><br><pre style="color:red">';
message += softTab + err.stack.replace(/\n/g, '<br>' + softTab);
message += '</pre>';
}
return renderFullPage(message, {});
}
Or maybe, using CSS instead of <br>
tags and "soft" tabs: 或者,也许使用CSS代替<br>
标签和“软”标签:
function renderError(err) {
let message = 'Server Error';
if (process.env.NODE_ENV !== 'production')
message += `:<pre style="margin-top:2em;padding-left:3em;color:red;">${err.stack}</pre>`;
return renderFullPage(message, {});
}
Here is the solution I came up with to suite airbrb style guide. 这是我想出的套件airbrb风格指南的解决方案。
const renderError = err => {
const softTab = '    ';
const errTrace = process.env.NODE_ENV !== 'production' ?
`:<br><br><pre style="color:red">${softTab}${err.stack.replace(/\n/g, `<br>${softTab}`)}</pre>` : '';
return renderFullPage(`Server Error${errTrace}`, {});
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.