1

I am rendering some div and I have like to not show a div when an image is not there.

An example image is here: https://clips-media-assets2.twitch.tv/AT-cm%7C891283246-preview-480x272.jpg

I am thinking of something like this:

<div v-show="twItem.imageurl">{{twItem.title}}</div>

But it doesn't work. Any help would be appreciated.

4
  • probably it because your component render faster than you passing image data to twItem, in this case you can define twItem as object something like this twItem: {} Commented Oct 24, 2020 at 15:20
  • @DevCl9 there is no error when I use v-if but it doesn't work. Commented Oct 24, 2020 at 15:50
  • 'is not there' means the url string is an empty string or the url links for an 404 image? Commented Oct 24, 2020 at 16:11
  • @dunhuang when I click on the link clips-media-assets2.twitch.tv/…, I am getting access denied, so it should be access denied. Commented Oct 29, 2020 at 19:37

2 Answers 2

1

This task is not primitive.

No matter if you use v-if or v-show, both compare the same thing, but the result is different. v-if="false" will not render the element at all, whilst v-show="false" will render it, but hidden. (display: none;)

The problem here is, that you simply check if the twItem.imageurl is set and NOT if the image was loaded.

What you might be able to do is using @load:

<template>
  <div v-if="twItem.loaded">{{ twItem.title }}</div>
  <image :src="twItem.imageurl" @load="twItem.loaded = true">
</template>

See here for a more detailed explanation: https://renatello.com/vue-js-image-loaded/

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

2 Comments

this would work (+1). OP may also use this article for more explanation
Thanks, didnt invest time to look for a thoughrough @load explanation. I added it.
0

Use v-if instead of v-show.

<div v-if="twItem.imageurl">{{ twItem.title }}</div>

5 Comments

Ooops, I've just edited my answer. Try again with :v-if. It should work.
: is a short-hand syntax for v-bind directive (HTML attribute manipulation). You're essentially writing v-bind:v-if= ....
:v-if is probably not valid
Yep, not just probably. :v-if is not valid.
There's no : for directives, user directly instead <div v-if="twItem.imageurl">{{twItem.title}}</div>

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.