javascript - vue successfully obtains a certain attribute of json data, but reports an error
曾经蜡笔没有小新
曾经蜡笔没有小新 2017-07-05 11:04:21
0
2
992

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.

 

曾经蜡笔没有小新
曾经蜡笔没有小新

reply all (2)
三叔

Because obtaining data is asynchronous, and after your template is mounted, your data has not been obtained yet, resulting indetail.rating.averagenot being defined

A better way is to define the values you reference in the template indata

data() { detail: { rating: { average: '' } } }
    过去多啦不再A梦

    You wrotev-if='detail.rating.average!=0'in the template, but when the component is initialized, the internal attribute of data isdetail: [], sodetail.ratingis 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 withinv-if.

      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!