我有一个导出的const,它接受两个参数。有了这两个参数,我想做一些if检查,并在命中时将结果设置为一个变量。
在导出的const中,我还有另一个带有模板字符串的const,我想在那里添加上一个if
语句的结果。类似这样的东西:
export const getTest = (typeX, typeY) => {
let testing = '';
if (typeX === 'x' && typeY === 'y') {
testing = 'y';
}
const testFormat = `
<div style='
color: white;
font-size: 1rem;
font-weight: bold;
background: {series.color};'
>
{point.y}
</div>
`;
return {
headerFormat: '',
testFormat,
useHTML: true,
padding: 0,
};
};
在testFormat
中,我有{point.y},它在当前位置运行良好,但我应该如何让测试(来自if语句(靠近它?
类似{testing}{point.y}
这个问题的解决方案是将${testing}
插入到testFormat
的模板字符串中,如下所示:
const testFormat = `
<div style='
color: white;
font-size: 1rem;
font-weight: bold;
background: {series.color};'
>
${testing} {point.y}
</div>
`;
通过将${testing}
添加到字符串中,可以将变量testing
的值插入到testFormat
的最终字符串值中的模板字符串中。