简体   繁体   English

如何将自己的 svg 图标插入 ionic 6?

[英]how can I insert own svg icons into ionic 6?

Hello how are you?你好吗? I've been trying to insert my own icons into svg for days, I've seen several blogs but they don't work for me.几天来,我一直在尝试将自己的图标插入 svg,我看过几个博客,但它们对我不起作用。 I'm using ionic version 6.12.2.我正在使用离子版本 6.12.2。 Does anyone know how I can insert my own svg icons?有谁知道如何插入我自己的 svg 图标?

I'm using Sass properties to handle my icons.我正在使用 Sass 属性来处理我的图标。

In my styles folder, I have a class like _icons-svg.scss , and a class named _mixins.scss在我的styles文件夹中,我有一个类似_icons-svg.scss和一个名为 _mixins.scss 的_mixins.scss

In my _icons-svg.scss :在我的_icons-svg.scss

.icon-svg {
  height: 46px;
  width: 46px;
  background-size: 100% 100% !important;
  &.my-first-svg {
    @include svg-icon('my-first-svg-icon');
    height: 22px;
    width: 12px;
  }
  &.my-second-svg {
    @include svg-icon('my-second-svg-icon');
    height: 22px;
    width: 12px;
  }
...

And in my _mixins.scss file, I have:在我的_mixins.scss文件中,我有:

@mixin svg-icon($icon-name) {
  background: url('src/assets/icons/svg/' + $icon-name + '.svg') no-repeat;
}

So replace in the url by the folder containing your svg files.因此,将 url 替换为包含 svg 文件的文件夹。 So my svg file looks like this in my svg folder:所以我的 svg 文件在我的 svg 文件夹中如下所示:

my-first-svg-icon.svg
my-second-svg-icon.svg

And then on my HTML I just have to call like this:然后在我的 HTML 上,我只需要这样调用:

<div class="icon-svg my-second-svg"></div>

Using a SASS mixin was a common way to do this in Ionic 3, but in Ionic 4/5 it's much much simpler.使用 SASS 混合是 Ionic 3 中执行此操作的常用方法,但在 Ionic 4/5 中要简单得多。 Like you can see in the Ionicons docs :就像您在Ionicons 文档中看到的那样:

Custom icons自定义图标

To use a custom SVG, provide its url in the src attribute to request the external SVG file.要使用自定义 SVG,请在 src 属性中提供其 url 以请求外部 SVG 文件。 The src attribute works the same as in that the url must be accessible from the webpage that's making a request for the image. src 属性的作用与 url 必须可以从请求图像的网页访问相同。 Additionally, the external file can only be a valid svg and does not allow scripts or events within the svg element.此外,外部文件只能是有效的 svg 并且不允许 svg 元素中的脚本或事件。

<ion-icon src="/path/to/external/file.svg"></ion-icon>

So for example you can put your own icons folder in the assets folder, like this:因此,例如,您可以将自己的图标文件夹放在assets文件夹中,如下所示:

src
  |- app
  |- ...
  |- assets
      |- icons
          |- user.svg
          |- ...

Which can be used then like this:然后可以像这样使用它:

 <ion-icon src="assets/icons/user.svg"></ion-icon>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM