admin管理员组

文章数量:1318569

I have a for-loop running in vue using v-for that loops through a media object to see if there are images stored. That works correctly, but now I want a div displayed below it saying "There is no media" if the object is empty. The text "There is no media" always displays though. Any help on this would be greatly appreciated.

Below is my code:

<v-flex>
                            <p><v-flex sm3 v-for="media in current_sample.media"  :key="media.id">
                                <v-btn @click="delete_image(media.id)">[X]</v-btn> <img :src="'/' + media.path" class="rotateImg" />
                            </v-flex>
                                <v-flex sm3 v-if="current_sample.media">There is no media</v-flex>
                            </p>
                        </v-flex>

I have a for-loop running in vue using v-for that loops through a media object to see if there are images stored. That works correctly, but now I want a div displayed below it saying "There is no media" if the object is empty. The text "There is no media" always displays though. Any help on this would be greatly appreciated.

Below is my code:

<v-flex>
                            <p><v-flex sm3 v-for="media in current_sample.media"  :key="media.id">
                                <v-btn @click="delete_image(media.id)">[X]</v-btn> <img :src="'https://orderinformation.s3.amazonaws./' + media.path" class="rotateImg" />
                            </v-flex>
                                <v-flex sm3 v-if="current_sample.media">There is no media</v-flex>
                            </p>
                        </v-flex>
Share Improve this question asked Oct 2, 2019 at 15:16 SJW525SJW525 512 silver badges11 bronze badges 2
  • Test specific property instead of object, like v-if="current_sample.media.path !== '' " – DedaDev Commented Oct 2, 2019 at 15:21
  • @Deda I did it now says this error: "Cannot read property 'path' of undefined" in console – SJW525 Commented Oct 2, 2019 at 15:31
Add a ment  | 

2 Answers 2

Reset to default 4

You can use Object.entries property :

 <v-flex sm3 v-if="!Object.entries(current_sample.media).length">There is no media</v-flex>

You can use Object.keys which is patible with all browsers

v-if="Object.keys(obj).length"

本文标签: javascriptHow to check if a vue object is empty in vue using vifStack Overflow