javascript 如何在AstroJS中使用SVG?

mwg9r5ms  于 2023-04-28  发布在  Java
关注(0)|答案(2)|浏览(183)

前往他们的Docs @ AstroDocs

  • Astro.js示例 *
  1. import imgReference from './image.png'; // imgReference === '/src/image.png'
  2. import svgReference from './image.svg'; // svgReference === '/src/image.svg'
  3. import txtReference from './words.txt'; // txtReference === '/src/words.txt'
  4. // This example uses JSX, but you can use import references with any framework.
  5. <img src={imgReference} alt="image description" />;

它们导入并引用文件。
我想做的事情:
1.移动/public和/src中的SVG以隔离这种情况。
1.将文件重命名为ui。svg来简化命名问题。
1.将svg作为组件导入。
似乎什么都不管用。有什么帮助吗?

  • 我的索引天文 *
  1. ---
  2. // import Settings from './icons/Settings.svg';
  3. import ui from '../ui.svg'; // moved here and renamed file to remove potential issues.
  4. ---
  5. <html lang="en">
  6. <head>
  7. <meta charset="utf-8" />
  8. <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
  9. <meta name="viewport" content="width=device-width" />
  10. <meta name="generator" content={Astro.generator} />
  11. <title>Mudriy | Home</title>
  12. </head>
  13. <body>
  14. <h1>Astro</h1>
  15. <!-- <img src={Settings} alt="ff"/> -->
  16. <img src={ui} />
  17. </body>
  18. </html>
ma8fv8wu

ma8fv8wu1#

导入路径以两个点而不是一个点开始,这会改变路径,使其与文档中的示例不同。
如果ui.svg位于src/ui.svg,则此代码应该可以工作

  1. import ui from './ui.svg';
uxhixvfz

uxhixvfz2#

这个问题似乎是一个bug

解决方案:

将.svg重命名为.astroui.astro),因为SVG在技术上能够成为它们自己的组件。

然后参照组件,就像使用组件一样。

  1. import UI from '../UI.astro'
  2. <UI />

相关问题