出現(xiàn)以下錯誤荆烈,說通俗點就是三層表達式a.b.c拯勉,在對象a中沒有對象b,那么讀取對象a.b.c中的值憔购,自然會報錯宫峦。如果是兩層表達式a.b則不會報錯,返回的是undefined玫鸟。
【根本原因】:
我們發(fā)現(xiàn)這里的info是vuex中state管理加載的數(shù)據(jù)导绷,異步調(diào)用顯示,然后vue渲染機制中:
異步數(shù)據(jù)先顯示初始數(shù)據(jù)屎飘,再顯示帶數(shù)據(jù)的數(shù)據(jù)
解決:
第一種方法:在三層表達式前加個v-if判斷?
備注:大多數(shù)時候會不成功
第二種方法:寫全數(shù)據(jù)結(jié)構(gòu)妥曲,避免獲取不存在的字段。
備注:百分之99可以解決
如:
storeData: {
? ? ? ? ? realData: { //實時數(shù)據(jù)
? ? ? ? ? ? current: { //當(dāng)前數(shù)據(jù)
? ? ? ? ? ? ? storeUserViews: 0, //店鋪訪客量
? ? ? ? ? ? ? productTotalSales: 0, //商品銷量
? ? ? ? ? ? ? productPageViews: 0, //商品瀏覽量
? ? ? ? ? ? ? storePageViews: 0 //店鋪瀏覽量
? ? ? ? ? ? },
? ? ? ? ? ? before: { //比較之前數(shù)據(jù)
? ? ? ? ? ? ? storeUserViews: 0,
? ? ? ? ? ? ? productTotalSales: 0,
? ? ? ? ? ? ? productPageViews: 0,
? ? ? ? ? ? ? storePageViews: 0
? ? ? ? ? ? }
? ? ? ? ? }
}