【问题标题】:Send props and convert image to svg using vue-svg-inline-loader in Vue在 Vue 中使用 vue-svg-inline-loader 发送道具并将图像转换为 svg
【发布时间】:2020-03-25 18:09:01
【问题描述】:

我使用vue-svg-inline-loader

在第一个组件中,我使用道具源发送到图像 svg:

<IconComponent :imgIcon="'../../assets/img/icon-example.svg'" />

接下来,当我得到道具时,我有第二个组件:

<img svg-inline :src="imgIcon" alt="icon">

export default {
   name: 'IconComponent',
   props: {
      imgIcon: {
         type: String,
         required: true
      }
   }
};

但库 vue-svg-inline-loader 无法将 img 转换为 svg。

我有:

当我在第二个组件中更改为:

<img svg-inline src="../../assets/img/icon-example.svg" alt="icon">

然后图像正确转换为 svg。

编辑:

我补充说:

<object :data="require(`@/assets/img/${this.name}.svg`)" type="image/svg+xml"></object>

图像转换成 svg,我在 DOM 中有这个结构。

现在我需要使用 SASS 更改颜色。我写代码:

.icon {
   margin: 0 map-get($margins, m1);
   display: flex;
   align-items: center;
   height: 100%;

   object svg g g {
      fill: red;
   }
}

但不会将颜色变为红色。

【问题讨论】:

    标签: vue.js svg


    【解决方案1】:

    根据这个问题,使用这个库时不可能有像:src这样的vue绑定:https://github.com/oliverfindl/vue-svg-inline-loader/issues/2

    comment 建议改用这个库:https://vue-svg-loader.js.org/

    编辑

    在我看来,一个不支持 VueJS 绑定的库是没有用的。你能切换到其他图书馆吗?这个看起来很有希望:https://github.com/seiyable/vue-simple-svg

    【讨论】:

    • 是的,我看到了这个存储库。但我不知道如何发送道具。在这个 repo 中,图片 svg 由import ... from ... 获取
    • 在您的代码中,您可以尝试:src="require(imgIcon)" 并检查它是否有效?
    • 对我的回答添加了编辑。我建议了另一个图书馆。
    猜你喜欢
    • 2023-01-20
    • 2021-09-03
    • 1970-01-01
    • 2020-05-29
    • 2020-01-03
    • 2018-09-28
    • 2017-12-17
    • 1970-01-01
    • 2020-03-27
    相关资源
    最近更新 更多