1、背景
由於Vue所生成的項目叫做單頁應用,即SPA,如果還是使用jQuery中的go(-1)或back()是行不通的,所以,我們使用到了Vue中的編程式導航。
2、基本使用
- 定義返回按鈕:
<el-button type="primary" plain @click="returnPage">{{$t('message.test.return')}}</el-button>
- 在methods中定義方法:
/** * [returnPage 返回按鈕] * @return {[type]} [description] */ returnPage() { // 跳轉上一級 // 這個判斷用來解決這種情況,當用戶沒有上一條路由的歷史記錄,出現點擊返回按鈕沒有反應時,下面的代碼用來判斷有沒有上一條路由的歷史記錄 如果沒有則返回首頁 if (window.history.length <= 1) { this.$router.push({ path: "/zh-CN/home" }); return false; } else { this.$router.go(-1); }
} - 在Jquery中,我們可以使用以下方法進行返回
window.history.go(-1)
3、遇到的問題
暫無