Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

139
Views
How can I load a SVG directly instead of importing via a js file for use in a leaflet divIcon?

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.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!