猿问

当某些对象具有字段而有些对象没有字段时,如何避免出现“ TypeError:

我正在通过向Google Places API发出GET请求来获取视觉信息。不幸的是,某些瞄准具opening_hours本身具有一个对象场,weekday_text而open_now某些瞄准具却没有,所以我得到了错误


<p v-if='sight.opening_hours && sight.opening_hours.open_now'>Open now</p>

<p v-if='sight.opening_hours && !sight.opening_hours.open_now'>Closed now</p>

<p v-if='sight.opening_hours && sight.opening_hours.weekday_text' v-for='day in this.sight.opening_hours.weekday_text'>{{ day }}</p>

我的v-if是否不涵盖存在sight.opening_hours和sight.opening_hours.weekday_text不存在的情况?我认为该错误来自v-for,因为如果我删除它,一切都会正常进行。


jeck猫
浏览 141回答 2
2回答

慕妹3146593

您需要重写代码,例如“外部条件检查和内部迭代”。您可能需要template元素。template(v-if="sight.opening_hours && sight.opening_hours.weekday_text")&nbsp; p(&nbsp; &nbsp; v-for="(day, index) in this.sight.opening_hours.weekday_text",&nbsp; &nbsp; :key="index"&nbsp; )&nbsp; &nbsp; | {{ day }}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答