亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Vue通過axios獲取數據,渲染的問題

Vue通過axios獲取數據,渲染的問題

慕妹3146593 2018-12-19 17:15:17
在學習慕課網vue購物車視頻里,遇到問題,在beforeMount里邊通過axios獲取數據,然后賦值給data里邊的數據,在comupted里邊使用forEach遍歷對象,都會出Cannot read property ‘forEach’ of null,這個錯誤。經過測試跟猜想,出那個錯誤是因為通過axios還沒獲取到數據,就開始遍歷對象,原因可能是因為axios獲取到對象再進行其它操作是異步的。如何才不會報錯呢?var vm=new Vue({    el:".shopcar",    data:{        productList:null        },    computed:{                total:function() {                    var money=0;                    this.productList.forEach(function(value){                        if(value.checked){                            money+=value.productPrice*value.productQuentity                        }                    })                    return money;                }    },    beforeMount:function() {            axios.get("data/cart.json").then((resq)=> {                this.productList=resq.data.result.productList;            })        },})
查看完整描述

1 回答

?
嚕嚕噠

TA貢獻1784條經驗 獲得超7個贊

this.productList && this.productList.forEach(function(value){


查看完整回答
反對 回復 2019-01-11
  • 1 回答
  • 0 關注
  • 1022 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號