{"id":3075,"date":"2024-11-10T14:06:57","date_gmt":"2024-11-10T06:06:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3075\/"},"modified":"2024-11-10T14:06:57","modified_gmt":"2024-11-10T06:06:57","slug":"vite-%e5%92%8c-react-%e4%b8%ad%ef%bc%8c%e8%a1%8c%e5%86%85%e6%a0%b7%e5%bc%8f-backgroundimage-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-%e7%ac%a6%e5%8f%b7%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3075\/","title":{"rendered":"Vite \u548c React \u4e2d\uff0c\u884c\u5185\u6837\u5f0f backgroundImage \u5982\u4f55\u4f7f\u7528 @ \u7b26\u53f7\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173098656572094.jpg\" class=\"aligncenter\" title=\"Vite \u548c React \u4e2d\uff0c\u884c\u5185\u6837\u5f0f backgroundImage \u5982\u4f55\u4f7f\u7528 @ \u7b26\u53f7\uff1f\u63d2\u56fe\" alt=\"Vite \u548c React \u4e2d\uff0c\u884c\u5185\u6837\u5f0f backgroundImage \u5982\u4f55\u4f7f\u7528 @ \u7b26\u53f7\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>vite \u642d\u914d react \u884c\u5185\u6837\u5f0f backgroundimage \u4e2d\u4f7f\u7528 @ \u7b26\u53f7<\/strong><\/p>\n<p>\u5728 vite \u548c react \u4e2d\uff0c\u884c\u5185\u6837\u5f0f\u4e2d\u4f7f\u7528 backgroundimage \u65f6\uff0curl() \u4e2d\u7684\u8def\u5f84\u901a\u5e38\u4f1a\u4f7f\u7528\u76f8\u5bf9\u4e8e\u5f53\u524d\u6a21\u5757\u7684\u4f4d\u7f6e\u3002\u4e3a\u4e86\u5c06\u76f8\u5bf9\u8def\u5f84\u66ff\u6362\u4e3a\u4f7f\u7528 @ \u7b26\u53f7\u7684\u522b\u540d\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u8bed\u53e5\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728\u884c\u5185\u6837\u5f0f\u4e2d\uff0c\u4f7f\u7528 require() \u6216 import() \u5c06\u56fe\u7247\u6a21\u5757\u5bfc\u5165\uff0c\u7136\u540e\u5c06\u5bfc\u5165\u540e\u7684\u56fe\u7247\u4f5c\u4e3a backgroundimage \u7684\u503c\u4f20\u9012\u3002<\/p>\n<p>\u4f7f\u7528 require()\uff1a<\/p>\n<pre>&lt;div style={{ backgroundimage: `url(${require('@\/assets\/1.jpg')})` }}&gt;asdsa&lt;\/div&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679c vite \u4e0d\u652f\u6301 require()\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 import() \u4ee3\u66ff\uff0c\u5e76\u4f7f\u7528 default \u5c5e\u6027\u83b7\u53d6\u56fe\u7247\u6a21\u5757\uff1a<\/p>\n<pre>import image from '@\/assets\/1.jpg';\n&lt;div style={{ backgroundImage: `url(${image.default})` }}&gt;asdsa&lt;\/div&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u901a\u8fc7\u4f7f\u7528 require() \u6216 import()\uff0c\u56fe\u7247\u6a21\u5757\u5c06\u88ab\u4f5c\u4e3a\u7edd\u5bf9\u8def\u5f84\u5bfc\u5165\uff0c\u5e76\u53ef\u4ee5\u6b63\u786e\u89e3\u6790 @ \u7b26\u53f7\u522b\u540d\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fVite \u548c React \u4e2d\uff0c\u884c\u5185\u6837\u5f0f backgroundImage \u5982\u4f55\u4f7f\u7528 @ \u7b26\u53f7\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>vite \u642d\u914d react \u884c\u5185\u6837\u5f0f backgroundimage \u4e2d\u4f7f\u7528 @ \u7b26\u53f7 \u5728 vite \u548c react \u4e2d\uff0c\u884c\u5185\u6837\u5f0f\u4e2d\u4f7f\u7528 backgroundimage \u65f6\uff0curl() \u4e2d\u7684\u8def\u5f84\u901a\u5e38\u4f1a\u4f7f\u7528\u76f8\u5bf9\u4e8e\u5f53\u524d\u6a21\u5757\u7684\u4f4d\u7f6e\u3002\u4e3a\u4e86\u5c06\u76f8\u5bf9\u8def\u5f84\u66ff\u6362\u4e3a\u4f7f\u7528 @ \u7b26\u53f7\u7684\u522b\u540d\uff0c\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u989d\u5916\u7684\u8bed\u53e5\u3002 \u89e3\u51b3\u65b9\u6cd5 \u5728\u884c\u5185\u6837\u5f0f\u4e2d\uff0c\u4f7f\u7528 require() \u6216 import() \u5c06\u56fe\u7247\u6a21\u5757\u5bfc\u5165\uff0c\u7136\u540e\u5c06\u5bfc\u5165\u540e\u7684\u56fe\u7247\u4f5c\u4e3a backgroundimage \u7684\u503c\u4f20\u9012\u3002 \u4f7f\u7528 require()\uff1a &lt;div style={{ backgroundimage: `url(${require(&#8216;@\/assets\/1.jpg&#8217;)})` }}&gt;asdsa&lt;\/div&gt; \u767b\u5f55\u540e\u590d\u5236 \u5982\u679c vite \u4e0d\u652f\u6301 require()\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528 import() \u4ee3\u66ff\uff0c\u5e76\u4f7f\u7528 default \u5c5e\u6027\u83b7\u53d6\u56fe\u7247\u6a21\u5757\uff1a import image from &#8216;@\/assets\/1.jpg&#8217;; &lt;div style={{ backgroundImage: `url(${image.default})` }}&gt;asdsa&lt;\/div&gt; \u767b\u5f55\u540e\u590d\u5236 \u901a\u8fc7\u4f7f\u7528 require() \u6216 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3075","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=3075"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3075\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}