Use vue to obtain the detailed information of a certain movie in Douban Movies. The data has been successfully obtained, and the average attribute is also displayed successfully on the page, but the console reports an error.
{{detail.title}}
({{detail.original_title}})
{{detail.rating.average}}分 暂无评分
Because obtaining data is asynchronous, and after your template is mounted, your data has not been obtained yet, resulting in
detail.rating.average
not being definedA better way is to define the values you reference in the template in
data
You wrote
v-if='detail.rating.average!=0'
in the template, but when the component is initialized, the internal attribute of data isdetail: []
, sodetail.rating
is undefined, so An error will occur when usingdetail.rating.average
.One solution is to define the detail data structure in data in advance according to the nested structure within
v-if
.