-
Notifications
You must be signed in to change notification settings - Fork 33
/
router.js
101 lines (90 loc) · 3.18 KB
/
router.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/**
* @file router
* @author mars
*/
import Vue from 'vue';
import VueRouter from 'vue-router';
import browserHistory from './browserHistory';
Vue.use(VueRouter);
const router = new VueRouter({
mode: 'history',
routes: []
});
function guid() {
return Math.random().toString(36).substr(2, 12);
}
Vue.prototype.isBack = false;
Vue.prototype.fromRouterPosY = 0;
router.beforeEach(function (to, from, next) {
if (!to.query._) {
to.query._ = guid();
}
let toPath = decodeURI(to.fullPath); // 兼容 vue-router 浏览器前进/返回时的 decodeURI操作
let fromPath = decodeURI(from.fullPath);
const browserHistoryArr = browserHistory.list;
const browserHistoryArrLength = browserHistoryArr.length;
const isBack = browserHistoryArrLength > 1
&& browserHistoryArr[browserHistoryArrLength - 1].path === fromPath
&& browserHistoryArr[browserHistoryArrLength - 2].path === toPath;
Vue.prototype.isBack = isBack;
if (!isBack) {
const query = to.query;
if (query._) {
delete query._;
}
}
toPath = decodeURI(to.fullPath);
fromPath = decodeURI(from.fullPath);
// 记录浏览历史
const browserHistoryLength = browserHistory.length;
let browserHistoryIndex = -1;
for (let i = browserHistoryLength; i >= 0; i--) {
if (browserHistoryArr[i] && browserHistoryArr[i].path === toPath) {
browserHistoryIndex = i;
}
}
for (let i = browserHistoryLength; i >= 0; i--) {
if (browserHistoryArr[i] && browserHistoryArr[i].path === fromPath) {
browserHistoryArr[i].pos = window.pageYOffset;
}
}
if (browserHistoryLength > 1
&& browserHistoryIndex <= browserHistoryLength - 2 && browserHistoryIndex >= 0
) {
browserHistoryArr.splice(
browserHistoryIndex + 1,
browserHistoryLength - browserHistoryIndex - 1
);
}
else {
// 进入无网络页后返回前一页面 会使得前一页面再次被push进browserHistoryArr, 需要避免这次重复push
if (browserHistory.length === 0
|| -1 === browserHistoryIndex
|| browserHistoryIndex !== browserHistoryLength - 1) {
browserHistoryArr.push({
path: toPath,
route: to.path.replace(/^\//, ''),
uri: to.path.replace(/^\//, ''),
options: to.query
});
}
}
// // 没有from.name 且 browserHistory.length !== 0,表示从畅听跳转到其他页面又跳转回畅听
// else {
// browserHistoryArr = [];
// browserHistoryArr.push({path: to.fullPath});
// }
Vue.prototype.currentRoute = browserHistoryArr;
browserHistory.set(browserHistoryArr);
router.app.$nextTick(() => {
const browserHistoryArr = browserHistory.list;
const browserHistoryLength = browserHistory.length;
const browserHistoryLast = browserHistoryArr[browserHistoryLength - 1];
if (toPath === browserHistoryLast.path
&& browserHistoryLast.pos) {
Vue.prototype.fromRouterPosY = browserHistoryLast.pos;
}
});
next();
});
export default router;