vue通過路由實現頁面重新整理的方法,vue頁面重新整理

來源:互聯網
上載者:User

vue通過路由實現頁面重新整理的方法,vue頁面重新整理

vue 開發商城項目,

需求如下:

購物車頁面跳轉到詳情頁,購物車頁麵包含了多個組件,點擊結算跳轉到訂單頁面,從訂單返回時,購物車頁面沒有重新整理,由於購物車組件之間通過bus實現事件傳遞,頁面跳轉(非物理返回)無法觸發beforeDestroy方法,則無法再此方法中銷毀bus方法

 beforeDestroy() {  this.$root.Bus.$off('judge')  this.$root.Bus.$off('refreshDetail')  this.$root.Bus.$off('clearAll')  this.$root.Bus.$off('upDataCart') },

無奈,通過beforeRouteLeave來銷毀

 beforeRouteLeave(to, from, next) {  this.$root.Bus.$off('judge')  this.$root.Bus.$off('refreshDetail')  this.$root.Bus.$off('clearAll')  this.$root.Bus.$off('upDataCart')  next() },

同樣的,物理返回時無法觸發購物車的created方法,則無法觸發bus的$on方法

歸根結底,物理返回時重新整理頁面則可以處理此問題

思路一

  beforeRouteEnter(to, from, next) {   next(()=>{     window.location.reload()   })  },

此方法理論貌似可行,但是頁面會狂刷不止,

最後採用了思路二,貌似是一個很low的方法,但是解決了實際問題

 this.$router.replace({ name: 'cart' })// 處理返回重新整理問題   this.$router.push({    path: '/order/order_sure',    query: {     sku: sku_str,     cart: 'cart'    }   })

頁面跳轉前先通過路由replace到當前頁,再跳轉到訂單頁面,返回可以自動重新整理了,這個方法並不理想,如果您有更好的方法,歡迎分享

有專門的方法處理此問題,在購物車頁面,添加如下代碼即可

 // 銷毀組件,返回重新整理 deactivated() {  this.$destroy() },

以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援幫客之家。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.