如何直接加载SVG,而不是通过JS文件导入以在传单Divicon中使用?

发布于 2025-02-12 20:28:15 字数 1380 浏览 0 评论 0 原文

我有一个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文件中的额外步骤。似乎这是不必要的。

I have a Vue 2 sample project at https://github.com/ericg-vue-questions/leaflet-test

I need to use this SVG inside of a leaflet divIcon.

      const cloudIcon = L.divIcon({
        html: thecloud, // this.cloudSvg, // thecloud,
        className: 'my-custom-icons',
        iconSize: [size, size],
        iconAnchor: [size/2, size/2]
      })

Additionally, I may need to make some modifications to the SVG, so I need the actual SVG source.

What does work is placing the SVG source inside of a javascript file and importing it by doing:

import {thecloud} from './TheCloud';

and I see:

working image

I did try:

  data() {
    return{
      cloudSvg: require('./TheCloud.svg')
    }
  },

But that did not work and I see:

not working

Is there a way to do this? I would like to avoid the extra step of placing the SVG source inside of javascript files. It seems like this should be unnecessary.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话墙 2025-02-19 20:28:15

我首选的解决方案是使用我所说的提取方法。

我所做的更改是:

  1. thecloud.svg 移动到 public/文件夹。
  2. 使用 fetch
const response = await fetch( "/TheCloud.svg");
const source = await response.text();

在嵌入源代码中的SVG时,在只有一个或少数svg时可以很好地工作,当SVG的数量变大时,我相信此方法是最好的解决方案。

我已经通过实施的提取方法更新了存储库。

My preferred solution is to use what I am calling the fetch-method.

The changes I made was to:

  1. move TheCloud.svg to the public/ folder.
  2. use the fetch to obtain the svg source
const response = await fetch( "/TheCloud.svg");
const source = await response.text();

While 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.

束缚m 2025-02-19 20:28:15

我找到了一种正在起作用的方法,但我相信有一些改进方法。

以下更改在 raw-loader

  1. YARN添加RAW-LOADER
  2. 创建 vue.config.js 在项目的根部使用以下内容,以配置 RAW-LOADER
module.exports = {
  chainWebpack: config => {
    config.module
      .rule('raw-loader')
      .test(/\.txt$/i)
      .use('raw-loader')
        .loader('raw-loader')
        .end()
  }
}
  1. 将我的 data()方法更改为:
  data() {
    return{
      center: [37.781814, -122.404740],
      cloudSvg: require('./TheCloud.svg'),
      cloudSrc: require('./TheCloud.txt')
    }
  },

添加 cloudsrc:require('./ thecloud.txt')

thecloud.txt thecloud.svg 的重复,但是使用不同的扩展名,因此 raw-loader 将处理它。

  1. divicon 的用法更改为:
      const cloudIcon = L.divIcon({
        html: this.cloudSrc.default, // thecloud, // this.cloudSvg, // thecloud,
        className: 'my-custom-icons',
        iconSize: [size, size],
        iconAnchor: [size/2, size/2]
      })

我不能说我了解这里发生的一切,例如为什么我需要 .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.

  1. yarn add raw-loader
  2. create a vue.config.js file at the root of the project with the following contents to configure the raw-loader.
module.exports = {
  chainWebpack: config => {
    config.module
      .rule('raw-loader')
      .test(/\.txt$/i)
      .use('raw-loader')
        .loader('raw-loader')
        .end()
  }
}
  1. Changed my data() method to:
  data() {
    return{
      center: [37.781814, -122.404740],
      cloudSvg: require('./TheCloud.svg'),
      cloudSrc: require('./TheCloud.txt')
    }
  },

adding cloudSrc: require('./TheCloud.txt').

TheCloud.txt is a duplicate of TheCloud.svg, but with a different extension so the raw-loader will process it.

  1. Change the usage of divIcon to:
      const cloudIcon = L.divIcon({
        html: this.cloudSrc.default, // thecloud, // this.cloudSvg, // thecloud,
        className: 'my-custom-icons',
        iconSize: [size, size],
        iconAnchor: [size/2, size/2]
      })

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文