> For AI agents: the complete documentation index is available at https://heyq02.com/llms.txt, the full documentation bundle is available at https://heyq02.com/llms-full.txt.

# React 中使用路径别名引用资源设置背景图

> 为了解决 url() 路径引入问题，将其转化成 base64 编码进而拼接字符串。

## 1. 问题背景

使用 css-in-js 为元素添加背景图时，会产生奇怪的 bug。

![](https://i-blog.csdnimg.cn/blog_migrate/3dd365d746f3c4b2c21be912d2fdf59d.png)

![](https://i-blog.csdnimg.cn/blog_migrate/2675570d2494ea77c9188c9f8036dbb7.png)

可以看到，路径前后多出了奇怪的引号。

![](https://i-blog.csdnimg.cn/blog_migrate/65061af79111cd9f02091bb33e4af081.png)

先路径别名引入资源，再拼接字符串。但是这样是使用了编码的方式。

![](https://i-blog.csdnimg.cn/blog_migrate/2ef175e5106224b9e1bf70304efd4448.png)

一种取巧的方式。
