|
@@ -0,0 +1,80 @@
|
|
|
+// 在router 方法中加拦截
|
|
|
+
|
|
|
+import router from '@/router'
|
|
|
+import store from '@/store';
|
|
|
+
|
|
|
+// 找到要跳转到的那个路由信息
|
|
|
+const getToRoute=(path)=>{
|
|
|
+ const allRoutes=router.getRoutes()
|
|
|
+ let routesItem={}
|
|
|
+ allRoutes.forEach(route => {
|
|
|
+ if(route.path===path){
|
|
|
+ routesItem=route
|
|
|
+ }
|
|
|
+ });
|
|
|
+ return routesItem
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+const routerPush=router.push //保存原来的push函数
|
|
|
+
|
|
|
+// 重写push函数
|
|
|
+router.push = function push(location) {
|
|
|
+ // location 可能是string 也可能是obj 完全取决于 你是怎么调push的
|
|
|
+ console.log('push');
|
|
|
+ console.log(location);
|
|
|
+
|
|
|
+ let path=''
|
|
|
+ let query=null
|
|
|
+ // 判断location是string 还是obj
|
|
|
+ if(typeof location === 'string'){
|
|
|
+ path=location
|
|
|
+ }else{
|
|
|
+ path=location.path
|
|
|
+ query=location.query
|
|
|
+ }
|
|
|
+
|
|
|
+ // 判断是否已经存在面包屑中
|
|
|
+ let index=-1
|
|
|
+ store.state.breadCrumbList.forEach((item,e) => {
|
|
|
+ if(item.path === path){
|
|
|
+ index=e
|
|
|
+ }
|
|
|
+ });
|
|
|
+ if(index===-1){//不存在
|
|
|
+ const routesItem=getToRoute(path)
|
|
|
+ store.commit('setBreadCrumb', routesItem)
|
|
|
+ }else{
|
|
|
+ console.log('拦截路由push改为返回');
|
|
|
+ const _index=index-(store.state.breadCrumbList.length-1)
|
|
|
+ router.go(_index)
|
|
|
+ setTimeout(() => {
|
|
|
+ if(query){
|
|
|
+ router.replace({
|
|
|
+ query:query
|
|
|
+ })
|
|
|
+ }else{
|
|
|
+ router.replace(path)
|
|
|
+ }
|
|
|
+ }, 10);
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // 调用原来的push函数,并捕获异常
|
|
|
+ return routerPush.call(this, location).catch(error => error)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+// 监听路由后退事件
|
|
|
+const listenRouterBack=()=>{
|
|
|
+ if(window.history&&window.history.pushState){{
|
|
|
+ $(window).on('popstate',()=>{
|
|
|
+ console.log('路由后退');
|
|
|
+ const path=window.location.pathname
|
|
|
+ const routesItem=getToRoute(path)
|
|
|
+ store.commit('setBreadCrumb', routesItem)
|
|
|
+ })
|
|
|
+ }}
|
|
|
+}
|
|
|
+listenRouterBack()
|