0

I'm having this problem and idk how fix this! Error in render:

TypeError: _vm.img.loadImg is not a function.

I wanna show the pic with a method loadImg(id), how I use this? I'm a novice in vue.

Code:


import Vue from 'vue'
import AsyncComputed from 'vue-async-computed'
Vue.use(AsyncComputed)

export default {
  name: "Sell",
  components: {
    Header
  },
  data() {
    return {
      divBar: DivBar,
      iconNext: IconNext,
      iconVaca: IconVaca,
      iconPeso: IconPeso,
      iconPreco: IconPreco,
      iconLocal: IconLocal,
      timelineItems: [],
      errorObj: {
        isSet: false
      },
      img: '',
    }
  },
  computed: {
    loadImg(id) {

      this.$http.get(this.$api + "/midiasvenda/" + id)
        .then(response => {
          if (response.success != null) {
            return response.success;
          } else {
            return Vaca
          }
        }).catch(error => {
          return error;
        })
      return Vaca;

    }
}
<template>
  <div v-else class="container-sell">
    <div v-for="item in timelineItems" :key="item.id" class="container-cow">
        <div class="left-content">
            <div class="flock-image">
                <img :src="img.loadImg(item.id)" alt="" class="cow">
            </div>
        </div>
   </div>
  </div>
</template>

Thanks for help guys!

6
  • Replace img.loadImg(item.id) with loadImg(item.id), because loadImg function doesn't exist on img. Commented Oct 31, 2018 at 14:20
  • the error continues: TypeError: _vm.loadImg is not a function Commented Oct 31, 2018 at 14:35
  • 1
    You should move your loadImg to methods. Commented Oct 31, 2018 at 14:37
  • okay, its works, but the image is not loaded, can you help me ? Commented Oct 31, 2018 at 14:41
  • Does this.$api+'/midiasvenda/'+id load the image itself? Commented Oct 31, 2018 at 14:43

2 Answers 2

2

The problem is with the line:

<img :src="img.loadImg(item.id)" alt="" class="cow">

Firstly you will need to change this to:

<img :src="loadImg(item.id)" alt="" class="cow">

Secondly, you can't have a computed property with a parameter. You'll need to create a method instead with the same functionality. Example:

methods: {
  loadImg(id) {
    this.$http.get(this.$api + "/midiasvenda/" + id)
      .then(response => {
        if (response.success != null) {
          return response.success;
        } else {
          return Vaca
        }
      }).catch(error => {
        return error;
      })
    return Vaca;
  }
}

Edit:

Here is a basic example

Sign up to request clarification or add additional context in comments.

Comments

0

The following line:

<img :src="img.loadImg(item.id)" alt="" class="cow">

should actually be:

<img :src="loadImg(item.id)" alt="" class="cow">

because the method loadImg(item.id)does not belong to the img data property.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.