在created()
的时候做数据请求:
created() {
this.getBanner()
this.getHotRecommended()
this.getNewAlbum(this.newAlbum.limit, this.newAlbum.offset)
this.getToplist()
},
methods: {
// 获取banner数据
getBanner() {
banner().then((res) => {
this.bannerList = res.data.banners
})
},
// 热门推荐
getHotRecommended() {
hotRecommended().then((res) => {
this.hotRecommendedList = res.data.result
})
},
// 获取新碟上架
getNewAlbum(limit, offset) {
newAlbum(limit, offset).then((res) => {
this.newAlbum.data = res.data.albums
})
},
// 榜单
getToplist() {
// 飙升榜
toplist(3).then((res) => {
this.biaosheng = res.data.result
})
// 新歌榜
toplist(0).then((res) => {
this.xinge = res.data.result
})
// 原创榜
toplist(2).then((res) => {
this.yuanchuang = res.data.result
})
}
}
在开始进入页面的时候报错,提示某个数据还没有:
[Vue warn]: Error in render function: "TypeError: Cannot read property 'coverImgUrl' of null"
但是页面加载完成后数据又正确加载了。
请问这是什么原因造成的,是请求数据的时机不对吗?
检查你的render函数,上面爆出来的是render function内的报错,跟你目前的执行的代码关系应该不大。
建议在访问对象属性时,做一下空值判断。如state && state.a && state.a.b