1

I have the following data

"meta": {
    "total_count": 17
},
"items": [
    {
        "id": 80,
        "meta": {
            "type": "",
            "detail_url": "",
            "html_url": "",
            "slug": "",
            "first_published_at": ""
        },
        "title": "some title",
        "image": {
            "id": 46,
            "meta": {
                "type": "wagtailimages.Image",
                "detail_url": "some url",
                "download_url": "/media/original_images/im.png"
            },
            "title": "im.png",
            "width": 100,
            "height": 80
        },
      }
    ]

Now if I try to get the width of an image inside :src attribute it works but if I try to pass it to a method it doesn't , a snippet of the HTML code is:

<li v-for="(item, index) in response_data">
  <a v-bind:href="item.meta.html_url">
    <div class="icon" :class="classObject(item.image.width,item.image.height)">
      <img v-if="item.image" v-bind:src="item.image.meta.download_url" alt=""/>
      <img v-else src="{% static 'images/logo-dummy.svg' %}" alt="logo-dummy.svg"/>
    </div>

where classObject is a method that compares the width and the height, I tried also to make it a computed property, the above code doesn't work but if for example I wrote

<img v-if="item.image" v-bind:src="item.image.width">

I can see the width inside the src attribute, what am I doing wrong? Edit: classObject code is the following

     classObject: function (width, height) {
        if (width > height) return 'icon1'
          return 'icon2'     
      },

5
  • 2
    What does classObject look like? How do you know it's not working? Commented Jan 19, 2021 at 5:34
  • Sorry, I'll edit the code now, and I know it's not working because the whole element is disappearing Commented Jan 19, 2021 at 5:40
  • 1
    Does the element disappear from the DOM? If not, what's the html output? Commented Jan 19, 2021 at 5:48
  • 1
    Seems to work fine here ~ jsfiddle.net/9ukz50sm. Use your browser's dev-tools to check for errors and to inspect the elements and their classes Commented Jan 19, 2021 at 5:51
  • it works Now!!! I'm sorry to bother you guys! Commented Jan 19, 2021 at 6:16

1 Answer 1

2

The problem I encountered was because sometimes there was no image at all, otherwise the code is correct so now I just check if there's an image like this

<div v-if="item.image" :class="classObject(item.image.width,item.image.height)">
      <img v-bind:src="item.image.meta.download_url" alt=""/>
    </div>

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

Comments

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.