2

I cannot render nested object content with v-for, there is a prop which contain object, but the div didn't show when i do v-if="prop". Please help how to solve it. Here is the syntax that i used for render:

<div v-if="statisticBrandBrowsers && statisticBrandBrowsers.length">
  <div v-for="(item, index) in statisticBrandBrowsers">
    <div>View: {{item.page_view.hits}}</div>
  </div>
</div>

My Props:

prop inspected on vue dev tool

1
  • Your statisticBrandBrowsers is an object instead of array so statisticBrandBrowsers.length is invalid. Better to have it as an array of browsers. Eg: statisticBrandBrowsers = [{browser: 'Chrome', conversion: {}, page_view: {}}]. Then you don't have to access the index as well. Commented Jan 10, 2019 at 5:42

1 Answer 1

4

The problem is inside the conditional rendering not inside v-for loop because the objects don't have a property called length, so you should do something like :

<div v-if="statisticBrandBrowsers && Object.values(statisticBrandBrowsers).length">

Object.values(statisticBrandBrowsers) will give you an array which has length property

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.