4

I want to show a div if the array length is more than 0. I used it below.

HTML:

<div v-if="countfunc > 0">
  <div v-for="nhp in countfunc">
     <p v-text="nhp.code"></p>
  </div>
</div>

Vue:

computed: {
   countfunc: function(){
     //this return a js array
   }
}

How can I solve my problem?

2
  • 1
    countfunc.length > 0 Commented Oct 17, 2020 at 18:33
  • @adiga Isn't that expensive, as in it calls the function twice. Commented Aug 13, 2022 at 16:15

2 Answers 2

5

You can use it as countfunc.length>0

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

Comments

0

The thing is, if countfunc is an empty array it will not show anything anyway, so this double check is a little bit unnecessary. Of course in your code you're forgetting the :key property in v-for as well.

Here you can try on codepen, try to delete the object inside the array and let it empty like

<script>
export default {
  data() {
    return {
      items: [],
    };
  },
};
</script>

And you can see that not will be rendered since the array is empty.

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.