Friday, May 17, 2024
194
rated 0 times [  197] [ 3]  / answers: 1 / hits: 25464  / 8 Years ago, wed, march 16, 2016, 12:00:00

I got an prefer-template error from eslint. For the workaround, I changed my code to use a template string inside the require function which is nested inside the url function as following:



{
background: `url(${require(`../../assets/${edge.node.name.toLowerCase()}.png` center no-repeat`)})
}


However, that gave an error, obviously. Here is the code I was using before, a plus sign concatenating inside the require function instead of the template string.



{
background: `url(${require('../../assets/' + edge.node.name.toLowerCase() + '.png')}) center no-repeat`
}


Would it be possible to define nested template strings?


More From » ecmascript-6

 Answers
7

Yes, it's possible, but you had for some reason put the )}) part (that closes the require call, the interpolated value, and the CSS url) in the wrong place:



{
background: `url(${require(`../../assets/${edge.node.name.toLowerCase()}.png`)}) center no-repeat`
// ^^^
}


That said, it's probably a bad idea as it doesn't exactly make the code readable. Better use a variable:



const bgurl = require(`../../assets/${edge.node.name.toLowerCase()}.png`);
… {
background: `url(${bgurl}) center no-repeat`
}

[#62927] Sunday, March 13, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
isham

Total Points: 69
Total Questions: 86
Total Answers: 86

Location: Anguilla
Member since Sun, Jan 29, 2023
1 Year ago
;