0

I am trying to concatenate 'assets' and to bind prod.productFileName to v-img :src

this works: <v-img :src="@/assets/abc.png" />

but I cannot do this: <v-img :src="@/assets/prod.productFileName" />

So I was trying to concatenate from the forEach bellow:

How can I do it?

<div v-for="prod in products" :key="prod.id">
  <v-img :src="prod.productFileName" />


<script>
methods: {
   getProducts() {
      axios.get("https://localhost:44397/api/Product/List")
        .then(res => {
          this.products = res.data;

           this.products.forEach(prod => {                         
             prod.productFileName = `@/assets/${prod.productFileName}`  // <--- I am getting an error
          });
        })         
    }
}
Eyal
  • 4,653
  • 9
  • 40
  • 56

2 Answers2

0

what you can do is store the response in this.products and no extra step is needed then in the img tag you can do is use template literals directly on the :src binding something like this

<div v-for="prod in products" :key="prod.id">
  <v-img :src="`/assets/${prod.productFileName}`" />


<script>
methods: {
   getProducts() {
      axios.get("https://localhost:44397/api/Product/List")
        .then(res => {
            this.products = res.data;
        })         
    }
}

and make sure that image is located in that location, otherwise the image will not be displayed

Rinor Dreshaj
  • 1,032
  • 12
  • 24
-1

You have change your code like below.

this.products.forEach(prod => {                         
   prod.productFileName = '/Uploads/' + prod.productFileName
});

Since this is a script you can simply append the string.

Stark Buttowski
  • 1,799
  • 2
  • 10
  • 21