<div class="noData" v-if="lifeList.length==0"> <img src="../assets/images/noData.png" alt=""> <p>這裏暫時尚未內容哦~親</p> </div> <mt-loadmore :top-method="loadTop" ref="loadTop" v-else> <div v-infinite-scroll="loadMore" infinite-scroll-disabled="loading" infinite-scroll-distance="10"> <LifeListItem :lists="lifeList" merchantType="user"></LifeListItem> <div class="loading-text" v-show="{loadingTextBtn:true}"> <span v-text="loadingText"></span> <mt-spinner v-if="(loadingComplete==false)" type="snake" :size="16"></mt-spinner> </div> </div> <div class="refreshComplete" v-if="refreshComplete==true">帖子已更新</div> </mt-loadmore>
因爲我設定的頁面默認進入的時候加載一次刷新函數,剛開始刷新完以後lifeList還不能及時更新過來,且我給該組件上加了v-else等判斷,因此會報錯:html
Error in mounted hook: "TypeError: Cannot read property 'onTopLoaded' of undefined"
獲取當前this.$refs能夠看到有這個dom元素 loadTop
可是this.$refs.loadTop打印則爲undefineddom
解決辦法:函數
loadTop(){ this.$store.dispatch('refreshLifeList', { city: this.city, country: this.country, category: this.category, page: 0, size: this.size }); this.page++; if(this.lifeList.length>0){ //這個判斷是新加的 this.$refs.loadTop.onTopLoaded(); } this.refreshComplete = true; setTimeout(() => { this.refreshComplete = false; }, 2000) },
給loadTop函數裏的this
this.$refs.loadTop.onTopLoaded(); 作一個判斷便可。或者在html中不要加v-if和v-else是否展現的判斷均可。