1

In Vue, I'm trying to pass a string into require(), the first option works (passing the string directly). however when passing the variable containing the string the loading fails, any clues???

<template>
    <img :src="require('@/assets/channels/email.png')" />
//   <img :src="require(test)" />
</template>

export default {
  data() {
    return {
      test: "@/assets/channels/email.png"
    };
  }
}
Yaniv Peretz
  • 1,118
  • 2
  • 13
  • 22
  • Possible duplicate of [Using require('...') with a variable vs. using a string in webpack](https://stackoverflow.com/questions/37241662/using-require-with-a-variable-vs-using-a-string-in-webpack) – CDT Apr 17 '19 at 09:16

1 Answers1

0

Maximum you could is a require with expression, with full variable it wont work. See docs here

<template>
    <img :src="require('@/assets/channels/'+test+'.png')" />
</template>

export default {
  data() {
    return {
      test: "email"
    };
  }
}
Aldarund
  • 17,312
  • 5
  • 73
  • 104