1

Good afternoon everyone. thanks for reading this. I am trying to populate a list of items. objects1 is an array of items with multiple data including an url for getting the item's picture. The issue is that I only see blank instead of the updated picture. When I click "back" (button to go to previous page), I am able to see all the pictures for a second before the previous page is shown.

 <template>
   <div v-for="item in itemList">
    <div class="item-picture" :style="{ 'backgroundImage' : 'url(' + 
      item.picture + ')' }"></div>
      <p>{{item.name}}</p>
      <p>{{item.address}}</p>
    </div>
  </div>
 </template>

<script>
   import axios from 'axios'

   export default {
   name: ‘name1’,
   props: [
      ‘objects1’
   ],
   data () {
     return {
        objects2: [],
     }
   },
   watch: {
    objects1: function () {
      this.objects2 = this.objects1
      for (let i = 0; i < this.objects2.length; i++) {
        this.getPic(this.objects2[i].url, i)
      }
    },
  },
  mounted () {
  },
  updated () {
  },
  methods: {
    getPic (url, index) {
      let _this = this
      axios.get(url)
      .then((res) => {
          _this.objects2[index].picture = res.data.url
        }
      })
      .catch((error) => {
      })
    },
  }
}

1 Answer 1

1

Try using Vue.set and see the change detection caveats from the docs (https://v2.vuejs.org/v2/guide/reactivity.html#Change-Detection-Caveats)

Instead of

_this.objects2[index].picture = res.data.url

Use

Vue.set(this.objects2[index], 'picture', res.data.url)
Sign up to request clarification or add additional context in comments.

1 Comment

thanks for your quick response. It works. I will read that link.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.