{"id":3233,"date":"2024-11-10T09:44:57","date_gmt":"2024-11-10T01:44:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/3233\/"},"modified":"2024-11-10T09:44:57","modified_gmt":"2024-11-10T01:44:57","slug":"vitereact%ef%bc%9a%e5%a6%82%e4%bd%95%e7%94%a8%e7%ac%a6%e5%8f%b7%e5%ae%9a%e4%b9%89%e8%a1%8c%e5%86%85%e6%a0%b7%e5%bc%8f%e4%b8%ad%e7%9a%84backgroundimage-url%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/3233\/","title":{"rendered":"Vite+React\uff1a\u5982\u4f55\u7528@\u7b26\u53f7\u5b9a\u4e49\u884c\u5185\u6837\u5f0f\u4e2d\u7684backgroundImage URL\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173102404991254.jpg\" class=\"aligncenter\" title=\"Vite+React\uff1a\u5982\u4f55\u7528@\u7b26\u53f7\u5b9a\u4e49\u884c\u5185\u6837\u5f0f\u4e2d\u7684backgroundImage URL\uff1f\u63d2\u56fe\" alt=\"Vite+React\uff1a\u5982\u4f55\u7528@\u7b26\u53f7\u5b9a\u4e49\u884c\u5185\u6837\u5f0f\u4e2d\u7684backgroundImage URL\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>vite+react\uff1a\u5982\u4f55\u7528@\u7b26\u53f7\u5b9a\u4e49\u884c\u5185\u6837\u5f0f\u4e2d\u7684backgroundimage url<\/strong><\/p>\n<p>\u5728react\u4e2d\uff0c\u4f7f\u7528\u884c\u5185\u6837\u5f0f\u65f6\uff0c\u5982\u4f55\u5c06backgroundimage url\u5b9a\u4e49\u4e3a@\u7b26\u53f7\uff1f<\/p>\n<p>\u4e3a\u4e86\u5728vite\u4e2d\u4f7f\u7528@\u7b26\u53f7\u5b9a\u4e49backgroundimage url\uff0c\u9700\u8981\u4f7f\u7528require\u51fd\u6570\u6216import\u8bed\u53e5\u3002<\/p>\n<p>\u4f7f\u7528require\u51fd\u6570\uff1a<\/p>\n<pre>&lt;div style={{ backgroundimage: `url(${require('@\/assets\/1.jpg')})` }}&gt;...&lt;\/div&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u5982\u679cvite\u4e0d\u652f\u6301require\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u56fe\u7247\uff1a<\/p>\n<pre>import img from '@\/assets\/1.jpg';\n&lt;div style={{ backgroundimage: `url(${img})` }}&gt;...&lt;\/div&gt;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u5916\uff0c\u5728vite.config.js\u4e2d\u914d\u7f6e\u522b\u540d\uff1a<\/p>\n<pre>  resolve: {\n    alias: {\n      '@': path.resolve(__dirname, '.\/src'),\n    },\n  },<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u8fd9\u6837\uff0c\u5373\u53ef\u5728\u884c\u5185\u6837\u5f0f\u4e2d\u4f7f\u7528@\u7b26\u53f7\u6765\u5b9a\u4e49backgroundimage url\uff0cvite\u4f1a\u81ea\u52a8\u89e3\u6790\u8def\u5f84\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fVite+React\uff1a\u5982\u4f55\u7528@\u7b26\u53f7\u5b9a\u4e49\u884c\u5185\u6837\u5f0f\u4e2d\u7684backgroundImage 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>vite+react\uff1a\u5982\u4f55\u7528@\u7b26\u53f7\u5b9a\u4e49\u884c\u5185\u6837\u5f0f\u4e2d\u7684backgroundimage url \u5728react\u4e2d\uff0c\u4f7f\u7528\u884c\u5185\u6837\u5f0f\u65f6\uff0c\u5982\u4f55\u5c06backgroundimage url\u5b9a\u4e49\u4e3a@\u7b26\u53f7\uff1f \u4e3a\u4e86\u5728vite\u4e2d\u4f7f\u7528@\u7b26\u53f7\u5b9a\u4e49backgroundimage url\uff0c\u9700\u8981\u4f7f\u7528require\u51fd\u6570\u6216import\u8bed\u53e5\u3002 \u4f7f\u7528require\u51fd\u6570\uff1a &lt;div style={{ backgroundimage: `url(${require(&#8216;@\/assets\/1.jpg&#8217;)})` }}&gt;&#8230;&lt;\/div&gt; \u767b\u5f55\u540e\u590d\u5236 \u5982\u679cvite\u4e0d\u652f\u6301require\uff0c\u5219\u53ef\u4ee5\u4f7f\u7528import\u8bed\u53e5\u5bfc\u5165\u56fe\u7247\uff1a import img from &#8216;@\/assets\/1.jpg&#8217;; &lt;div style={{ backgroundimage: `url(${img})` }}&gt;&#8230;&lt;\/div&gt; \u767b\u5f55\u540e\u590d\u5236 \u6b64\u5916\uff0c\u5728vite.config.js\u4e2d\u914d\u7f6e\u522b\u540d\uff1a resolve: { alias: { &#8216;@&#8217;: path.resolve(__dirname, &#8216;.\/src&#8217;), }, }, \u767b\u5f55\u540e\u590d\u5236 \u8fd9\u6837\uff0c\u5373\u53ef\u5728\u884c\u5185\u6837\u5f0f\u4e2d\u4f7f\u7528@\u7b26\u53f7\u6765\u5b9a\u4e49backgroundimage url\uff0cvite\u4f1a\u81ea\u52a8\u89e3\u6790\u8def\u5f84\u3002 \u4ee5\u4e0a\u5c31\u662fVite+React\uff1a\u5982\u4f55\u7528@\u7b26\u53f7\u5b9a\u4e49\u884c\u5185\u6837\u5f0f\u4e2d\u7684backgroundImage 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},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-3233","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3233","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=3233"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/3233\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=3233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=3233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=3233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}