{"id":26855,"date":"2024-11-24T09:37:46","date_gmt":"2024-11-24T01:37:46","guid":{"rendered":"https:\/\/fwq.ai\/blog\/26855\/"},"modified":"2024-11-24T09:37:46","modified_gmt":"2024-11-24T01:37:46","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8-vite-%e4%b8%8e-react-%e4%b8%ad%e4%bd%bf%e7%94%a8%e5%b8%a6%e6%9c%89-%e7%ac%a6%e5%8f%b7%e7%9a%84%e5%86%85%e8%81%94%e6%a0%b7%e5%bc%8f-backgroundimage-url%ef%bc%9f-2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/26855\/","title":{"rendered":"\u5982\u4f55\u5728 Vite \u4e0e React \u4e2d\u4f7f\u7528\u5e26\u6709 @ \u7b26\u53f7\u7684\u5185\u8054\u6837\u5f0f backgroundImage URL\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173111473789839.jpg\" class=\"aligncenter\" title=\"\u5982\u4f55\u5728 Vite \u4e0e React \u4e2d\u4f7f\u7528\u5e26\u6709 @ \u7b26\u53f7\u7684\u5185\u8054\u6837\u5f0f backgroundImage URL\uff1f\u63d2\u56fe\" alt=\"\u5982\u4f55\u5728 Vite \u4e0e React \u4e2d\u4f7f\u7528\u5e26\u6709 @ \u7b26\u53f7\u7684\u5185\u8054\u6837\u5f0f backgroundImage URL\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 vite \u642d\u914d react \u4e2d\u4f7f\u7528\u5e26\u6709 @ \u7b26\u53f7\u7684\u5185\u8054\u6837\u5f0f backgroundimage url<\/strong><\/p>\n<p>\u5728 vite \u4e0e react \u4e2d\uff0c\u901a\u8fc7\u5185\u8054\u6837\u5f0f\u8bbe\u7f6e backgroundimage \u65f6\uff0c\u9700\u8981\u627e\u5230\u4e00\u79cd\u65b9\u6cd5\u5c06 @ \u7b26\u53f7\u89e3\u6790\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528 require \u51fd\u6570\u6216 import \u8bed\u53e5\u5728\u811a\u672c\u4e2d\u52a8\u6001\u52a0\u8f7d\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9 backgroundimage \u5c5e\u6027\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\u53ef\u4ee5\u6539\u7528 import \u6765\u5bfc\u5165\u56fe\u7247\uff1a<\/p>\n<pre>import image from '@\/assets\/1.jpg';\n\n&lt;div style={{ backgroundImage: `url(${image})` }}&gt;asdsa&lt;\/div&gt;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6ce8\u610f\uff1a<\/strong><\/p>\n<ul>\n<li>require \u51fd\u6570\u6216 import \u8bed\u53e5\u5fc5\u987b\u5728\u7ec4\u4ef6\u7684\u51fd\u6570\u5185\u4f7f\u7528\u3002<\/li>\n<li>require \u51fd\u6570\u4ec5\u5728\u6784\u5efa\u65f6\u6709\u6548\uff0c\u5728\u5f00\u53d1\u6a21\u5f0f\u4e0b\u4e0d\u5de5\u4f5c\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5982\u4f55\u5728 Vite \u4e0e React \u4e2d\u4f7f\u7528\u5e26\u6709 @ \u7b26\u53f7\u7684\u5185\u8054\u6837\u5f0f backgroundImage URL\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>\u5982\u4f55\u5728 vite \u642d\u914d react \u4e2d\u4f7f\u7528\u5e26\u6709 @ \u7b26\u53f7\u7684\u5185\u8054\u6837\u5f0f backgroundimage url \u5728 vite \u4e0e react \u4e2d\uff0c\u901a\u8fc7\u5185\u8054\u6837\u5f0f\u8bbe\u7f6e backgroundimage \u65f6\uff0c\u9700\u8981\u627e\u5230\u4e00\u79cd\u65b9\u6cd5\u5c06 @ \u7b26\u53f7\u89e3\u6790\u4e3a\u7edd\u5bf9\u8def\u5f84\u3002 \u89e3\u51b3\u65b9\u6848\uff1a \u4f7f\u7528 require \u51fd\u6570\u6216 import \u8bed\u53e5\u5728\u811a\u672c\u4e2d\u52a8\u6001\u52a0\u8f7d\u56fe\u50cf\uff0c\u5e76\u5c06\u5176\u4f20\u9012\u7ed9 backgroundimage \u5c5e\u6027\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\u53ef\u4ee5\u6539\u7528 import \u6765\u5bfc\u5165\u56fe\u7247\uff1a import image from &#8216;@\/assets\/1.jpg&#8217;; &lt;div style={{ backgroundImage: `url(${image})` }}&gt;asdsa&lt;\/div&gt; \u767b\u5f55\u540e\u590d\u5236 \u6ce8\u610f\uff1a require \u51fd\u6570\u6216 import \u8bed\u53e5\u5fc5\u987b\u5728\u7ec4\u4ef6\u7684\u51fd\u6570\u5185\u4f7f\u7528\u3002 require \u51fd\u6570\u4ec5\u5728\u6784\u5efa\u65f6\u6709\u6548\uff0c\u5728\u5f00\u53d1\u6a21\u5f0f\u4e0b\u4e0d\u5de5\u4f5c\u3002 [&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-26855","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26855","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=26855"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/26855\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=26855"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=26855"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=26855"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}