如何直接加载SVG,而不是通过JS文件导入以在传单Divicon中使用?
我有一个vue 2示例项目,位于 https://github.com/ericg-vue-vue-vue-com-问题/传单检验
我需要在传单内使用此SVG divicon 。
const cloudIcon = L.divIcon({
html: thecloud, // this.cloudSvg, // thecloud,
className: 'my-custom-icons',
iconSize: [size, size],
iconAnchor: [size/2, size/2]
})
此外,我可能需要对SVG进行一些修改,因此我需要实际的SVG源。
什么工作是将SVG源放置在A javaScript文件并通过执行
import {thecloud} from './TheCloud';
:
我确实尝试过:
data() {
return{
cloudSvg: require('./TheCloud.svg')
}
},
但是那没有起作用,我看到了:
有办法做到这一点吗?我想避免将SVG源放置在JavaScript文件中的额外步骤。似乎这是不必要的。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我首选的解决方案是使用我所说的提取方法。
我所做的更改是:
thecloud.svg
移动到public/
文件夹。fetch
在嵌入源代码中的SVG时,在只有一个或少数svg时可以很好地工作,当SVG的数量变大时,我相信此方法是最好的解决方案。
我已经通过实施的提取方法更新了存储库。
My preferred solution is to use what I am calling the fetch-method.
The changes I made was to:
TheCloud.svg
to thepublic/
folder.fetch
to obtain the svg sourceWhile embedding SVGs in the source code works well when there is just a single or a small number of SVGs, when the number of SVGs becomes large, I believe this method is among the best solutions.
I have updated the repo with the fetch-method implemented.
我找到了一种正在起作用的方法,但我相信有一些改进方法。
以下更改在 raw-loader 。
YARN添加RAW-LOADER
vue.config.js
在项目的根部使用以下内容,以配置RAW-LOADER
。data()
方法更改为:添加
cloudsrc:require('./ thecloud.txt')
。thecloud.txt
是thecloud.svg
的重复,但是使用不同的扩展名,因此raw-loader
将处理它。divicon
的用法更改为:我不能说我了解这里发生的一切,例如为什么我需要
.default
part或webpack配置部分,但这是有效的。I found one method that is working, but I am sure there are ways to improve it.
The following changes are on the raw-loader branch.
yarn add raw-loader
vue.config.js
file at the root of the project with the following contents to configure theraw-loader
.data()
method to:adding
cloudSrc: require('./TheCloud.txt')
.TheCloud.txt
is a duplicate ofTheCloud.svg
, but with a different extension so theraw-loader
will process it.divIcon
to:I cannot say I understand everything going on here, like why I need the
.default
part or the webpack configuration section, but this is working.