Javascript 如何正确使用v-if和v-for嵌套对象vue js
我无法使用v-for呈现嵌套对象内容,有一个包含对象的道具,但当我使用v-if=“prop”时,div没有显示。请帮助解决这个问题。以下是我用于渲染的语法: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
<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',转换:{},页面视图:{}]
。这样您就不必访问索引了。