vue路由回退判斷


在頁面一開始加上一個全局的函數:
activated: function () {
  this.$setgoindex()
}

  

這個函數是這樣的,判斷當前頁面的歷史記錄是不是小於等於1,如果小於等於1,說明這個頁面沒有可以返回的上一頁,如果沒有可以返回的上一頁,就給地址欄加上一個goindex=true的參數,這樣你從這個頁面在往下一個頁面跳轉在返回,這個參數就一直加上的
Vue.prototype.$setgoindex = function () {
  if (window.history.length <= 1) {
    if (location.href.indexOf('?') === -1) {
      window.location.href = location.href + '?goindex=true'
    } else if (location.href.indexOf('?') !== -1 && location.href.indexOf('goindex') === -1) {
      window.location.href = location.href + '&goindex=true'
    }
  }
}

  

然后在左上角返回按鈕加上這個邏輯:
if (this.$route.query.goindex === 'true') {
  this.$router.push('/')
} else {
  this.$router.back(-1)
}

  

這樣就可以了


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM