简体   繁体   English

如何动态内联通过文件加载器加载的SVG?

[英]How do I dynamically inline an SVG loaded via file-loader?

I was using svg-inline-loader to inline my SVGs which worked perfectly. 我正在使用svg-inline-loader内联我的svg-inline-loader ,效果很好。

However, I also need to use those SVGs elsewhere not inline so I cannot using svg-inline-loader . 但是,我还需要在非内联的其他地方使用这些svg-inline-loader因此无法使用svg-inline-loader I need to use file-loader to import my SVGs. 我需要使用file-loader来导入我的SVG。

It seems like it would be easy to inline my SVGs, there are multiple components available that do just that. 内联我的SVG似乎很容易,有很多组件可以做到这一点。 However, I also need to do it dynamically based on the name of the icon required. 但是,我还需要根据所需图标的名称动态地执行此操作。

This makes it awkward as I can't find a way to dynamically import a file and inline it on the page. 这让我很尴尬,因为我找不到动态导入文件并将其内联在页面上的方法。

Using an import() doesn't work. 使用import()不起作用。 Neither does require() . 都不require()

<template>
    <span class="s-icon" v-bind:class="[typeAndSize, optionClasses]" v-html="icon"></span>
</template>

computed: {
    icon () {
        return () => import(`../../../../node_modules/my-lib/images/svg-icons/${this.name}.svg`);
    },

Has anyone tackled this issue before? 有人解决过这个问题吗? Or can anyone point me in the right direction. 或者谁能​​指出我正确的方向。

Edit 编辑

Further to my comment on the accepted answer this is the full solution I ended up with: 除了我对已接受答案的评论之外,这是我最终得到的完整解决方案:

const icon = require(`!!svg-inline-loader!../../../../node_modules/my-lib/images/svg-icons/${this.name}.svg`);

You can use inline query string format, in order to apply only specific loader and ignore the rest. 您可以使用内联查询字符串格式,以便仅应用特定的加载程序,而忽略其余的加载程序。

const icon = require('!!file-loader!./icon.svg')

For dynamic imports you may look to require-context 对于动态导入,您可能需要使用require-context

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

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