You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Material-UI styled components报错:严格模式仅支持'\0'数字转义

解决"The only valid numeric escape in strict mode is '\0'"报错

在styled-components中设置伪元素content时,使用\201c这类数字转义字符会触发严格模式下的转义报错,核心问题是JavaScript与CSS的转义规则不兼容:

报错原因

JavaScript严格模式仅允许\0作为合法的数字转义序列,其他形式的数字/八进制转义(如\201c)会被判定为非法。你写的\201c会被JS尝试解析为无效的八进制转义,从而抛出错误。

三种可行解决方案

  • 方案1:使用JavaScript Unicode转义序列
    用\u开头的四位十六进制转义格式,让JS正确解析目标字符:

    export const Title = styled(Typography)(({ theme }) => ({
      color: "black",
      "&:before": {
        content: "\u201c",
      },
    }));
    
  • 方案2:转义反斜杠让CSS处理转义
    在JS字符串中用\\转义反斜杠,将原始转义符传递给CSS后由CSS解析:

    export const Title = styled(Typography)(({ theme }) => ({
      color: "black",
      "&:before": {
        content: "\\201c",
      },
    }));
    
  • 方案3:直接写入目标字符
    直接使用左双引号“,无需转义,代码可读性更高:

    export const Title = styled(Typography)(({ theme }) => ({
      color: "black",
      "&:before": {
        content: "“",
      },
    }));
    

内容的提问来源于stack exchange,提问作者Shoppe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 05:30:58