Ionic 5 / React:使用 ion-icon 嵌入自定义 SVG

Ionic 5 / React: Use ion-icon to embed custom SVG

我在 Ionic 5 中有一个 React 应用程序,我想向其中添加一些自定义 SVG。

但我的问题是关于 React 的。

我的应用程序文件夹结构如下所示:

这里是SvgListen.tsx:

import React from 'react';
import { ReactComponent as ListenSvg } from './listen.svg';
import { IonIcon } from '@ionic/react';

const SvgListen = () => (
  <>
    <ListenSvg />
    <IonIcon src="./listen.svg" font-size="48px" />

  </>
);

export default SvgListen;

在 Chrome 中测试应用程序时,我得到了这个 HTML:

<ion-icon src="./listen.svg" font-size="48px" role="img" class="ios hydrated"></ion-icon>

我导入的<ListenSvg />显示正确;但是,不显示离子图标。也没有报错。

我检查了 this blog post,但那里概述的方法也没有成功。

如何在 Ionic 5 中使用 <IonIcon> 显示自定义 SVG?

根据 Create React App docs 您可以导入图像以在输出包中获取它们的最终路径:

import React from 'react';
import { IonIcon } from '@ionic/react';
import listenSvg from './listen.svg';

const SvgListen = () => (
  <IonIcon src={listenSvg} font-size="48px" />
);

export default SvgListen;