Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vue.js/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何正确使用v-if和v-for嵌套对象vue js_Javascript_Vue.js_Vuejs2_Vue Component_Vuex - Fatal编程技术网

Javascript 如何正确使用v-if和v-for嵌套对象vue js

Javascript 如何正确使用v-if和v-for嵌套对象vue js,javascript,vue.js,vuejs2,vue-component,vuex,Javascript,Vue.js,Vuejs2,Vue Component,Vuex,我无法使用v-for呈现嵌套对象内容,有一个包含对象的道具,但当我使用v-if=“prop”时,div没有显示。请帮助解决这个问题。以下是我用于渲染的语法: <div v-if="statisticBrandBrowsers && statisticBrandBrowsers.length"> <div v-for="(item, index) in statisticBrandBrowsers"> <div>View: {{ite

我无法使用v-for呈现嵌套对象内容,有一个包含对象的道具,但当我使用v-if=“prop”时,div没有显示。请帮助解决这个问题。以下是我用于渲染的语法:

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

视图:{{item.page_View.hits}
我的道具:


问题出在条件渲染内部,而不是
v-for
循环内部,因为对象没有名为
length
的属性,因此您应该执行以下操作:

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


Object.values(StatisticBrandBrowser)
将为您提供一个具有
length
属性的数组

您的
StatisticBrandBrowser
是一个对象而不是数组,因此
StatisticBrandBrowser.length
无效。最好将它作为一个浏览器阵列。例如:
statisticBrandBrowsers=[{browser:'Chrome',转换:{},页面视图:{}]
。这样您就不必访问
索引了。