以下代码包含字符串中的字符串。由于 es6 类的主体处于严格模式,因此无法编译,因为内部字符串看起来包含八进制值。
class Test {
static get template () { return `
.class {
content: "\2713\0020"
}
`}
}
解决方法包括转义字符串或使用非 unicode 形式的字符。
我的工作流程包括在 fiddle 中创建 HTML 和 CSS,然后将代码复制粘贴到封装的 es6 类中(我仅限于使用单个文件)。
我可以做些什么来避免这个错误吗?我希望将字符串换行,但它仍在类体内。
最佳答案
您可以将 String.raw
与模板文字一起使用,以便反斜杠被解释为文字反斜杠,而不是转义字符:
class Test {
static get template () { return String.raw`
.class:before {
content: "\2713\0020"
}
`}
}
document.body.appendChild(document.createElement('style')).textContent = Test.template;
<div class="class">text</div>
(请注意,content
仅适用于 :before
and :after
psuedo-elements)
关于javascript - False '0' 嵌入字符串中带有前缀的八进制文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60560674/