2

I am having a problem setting dynamic image paths with Vue.js. I am using the Vue-Cli to build the project.

I believe the issue is caused because I am referencing image paths dynamically after runtime. Normally it seems the references to my ./assets/ folder are converted into ./img/ after runtime. Since I am changing the url dynamically after load the paths don't appear to work/load. Country is initially set via a store getters but is then v-modeled from a language select dropdown, where the values correspond the the url suffix.

    <div id="flag-container" :style="{ backgroundImage: `url(${src})` }"></div>

    computed: {
            src(){
                return `./assets/flags/flag-${this.country}.png`;
            }
        },

    data() {
        return {
            country: this.$store.getters.language
        }
    }

Inspector shows url change is implemented.

Any recommendation on the best solution for this?

9
  • In devtools did you inspect the network tab? Are the images linked properly? Do they exist on the server? Inspector shows url change is implemented. so when you inspect the dom, you can see that url() is correctly set when you change country? Commented Jan 10, 2019 at 22:13
  • Can you add example of rendered <div id="flag-container" ...> Commented Jan 10, 2019 at 22:13
  • <div id="flag-container" style="background-image: url("./assets/flags/flag-ru-RU.png");"></div> Yes, I see the change implemented in the HTML but get no render of the image. Commented Jan 10, 2019 at 23:05
  • 2
    please check this answer or this one Commented Jan 10, 2019 at 23:12
  • 2
    @MichaelPaccione not only individual images, just add require, check this webpack docs section webpack.js.org/guides/dependency-management/… Commented Jan 11, 2019 at 9:39

2 Answers 2

2

Using webpack require context and beforeMount, I was able to store the images in base64 format inside an object. I stored the object and accessed it with a dynamic key. Thanks Max for leading me to the right documentation.

export default {
  beforeMount() {
    var that = this;
    function importAll(r) {
      r.keys().forEach((key) => (that.imgCache[key] = r(key)));
    }

    importAll(require.context("../assets/flags/", true, /\.png$/));
  },
  computed: {
    src() {
      var key = `./flag-${this.country}.png`,
        url = this.imgCache[key];

      return url;
    },
  },
};
Sign up to request clarification or add additional context in comments.

Comments

0

Don't use

data() {
    return {
        country: this.$store.getters.language
    }
}

as it will stop being reactive to store changes. Use computed property

<div id="flag-container" :style="{ backgroundImage: `url(${src})` }"></div>

computed: {
    src(){
        return `./assets/flags/flag-${this.country}.png`;
    },
    country() {
        return this.$store.getters.language
    }
},

1 Comment

I understand what you are saying with your data flow... having the store be the master source and updating from that. What I have on this component instead is local data values that are v-modeled and then @change I update the store. So it is not from what I can see the issue as I am seeing the URL change when i change my select.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.