在頁面一開始加上一個全局的函數:
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) }
這樣就可以了