-
Notifications
You must be signed in to change notification settings - Fork 0
/
base-router.html
201 lines (186 loc) · 7.18 KB
/
base-router.html
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<button onclick="routerHistory.push('/')">首页</button>
<button onclick="routerHistory.push('/about')">关于</button>
<button onclick="routerHistory.replace('/xxx')">替换当前路由</button>
<script>
/**
* 目前浏览器都支持了 `history.pushState()`,可以使用该 API 实现两种路由模式。
* 路由系统应至少包含以下功能:
* 1. 读取当前的路径
* 2. 读取当前路径下的状态
* 3. 切换路径的方法:push、replace
* 4. 实现路由监听,如果路径变化,需要通知用户
*/
function buildState(
back,
current,
forward,
replace = false,
computedScroll = false
) {
return {
back,
current,
forward,
replace,
// 缓存scroll之后,可以利用 window.scrollBy(pageXOffset, pageYOffset); 将页面滚动到原来的位置
scroll: computedScroll
? { left: window.pageXOffset, top: window.pageYOffset }
: null,
position: window.history.length - 1,
};
}
function createCurrentLocation(base) {
const { pathname, search, hash } = window.location;
if (base.indexOf("#") > -1) {
// 如果是hash路由,createCurrentLocation 返回 # 后面的部分
return base.slice(1) || "/";
}
return pathname + hash + search;
}
function useHistoryStateNavgation(base) {
// 路由路径
const currentLocation = {
value: createCurrentLocation(base),
};
// 路由状态
const historyState = {
value: window.history.state,
};
// 第一次打开页面,window.history.state 为 null,就自己维护一个状态(后退的路径、当前的路径、要去的路径、是push跳转还是repalce跳转、跳转后的滚动条位置),并通过 history.replaceState 替换掉当前的状态
if (!historyState.value) {
changeLocation(
currentLocation.value,
buildState(null, currentLocation.value, null, true),
true
);
}
// 跳转并更新状态
function changeLocation(to, state, replace = false) {
const url = base.indexOf("#") > -1 ? base + to : to;
// window.history.replaceState/pushState 传入的 state 会修改掉 window.history.state,但是 historyState.value 需要手动修改为 state
window.history[replace ? "replaceState" : "pushState"](
state,
null,
url
);
historyState.value = state;
}
function push(to, data) {
const currentState = Object.assign({}, historyState.value, {
forward: to,
scroll: { left: window.pageXOffset, top: window.pageYOffset },
});
/**
* 下方location的第一个参数 to 为 当前路径 currentState.current;第三个参数为 true,即使用 history.replaceState 替换掉当前路由;
* 所以这里本质并没有跳转,【只是更新了当前状态】,后续在vue中我们可以详细监听到状态的变化
* 这一步的目的是为了实现在将要跳转前,触发生命周期钩子
*/
changeLocation(currentState.current, currentState, true);
// 创建 history.pushState 需要传入的 state,并实现真正的跳转
const state = Object.assign(
{},
buildState(currentLocation.value, to, null, false),
{ position: currentState.position + 1 },
data
);
console.log("push跳转时传入的state", state);
changeLocation(to, state, false); // 真正的跳转
currentLocation.value = to;
}
function replace(to, data) {
// 创建 history.replaceState 需要传入的 state,并实现真正的跳转
const state = Object.assign(
{},
buildState(
historyState.value.back,
to,
historyState.value.forward,
true
),
data
);
console.log("replace跳转时传入的state", state);
changeLocation(to, state, true); // 跳转并更新history的状态
currentLocation.value = to;
}
return {
location: currentLocation,
state: historyState,
push,
replace,
};
}
// 前进、后退的时候,要更新 historyState 和 currentLocation
function useHistoryListeners(base, historyState, currentLocation) {
let listeners = [];
// popstate 回调函数中的 state 是已经前进或后退完毕后的最新状态
const popStateHandler = ({ state }) => {
const to = createCurrentLocation(base);
const from = currentLocation.value;
const fromState = historyState.value;
// 点击前进/后退按钮后,修改当前的路径和状态
currentLocation.value = to;
historyState.value = state; // state 可能为 null
let isBack = state.position - fromState.position < 0;
// 监听到前进/后退,执行所有的listener
listeners.forEach((listener) => {
listener(to, from, { isBack });
});
};
window.addEventListener("popstate", popStateHandler); // 监听浏览器的前进、后退
function listen(cb) {
listeners.push(cb);
}
return {
listen,
};
}
function createWebHistory(base = "") {
const historyNavgation = useHistoryStateNavgation(base);
const historyListeners = useHistoryListeners(
base,
historyNavgation.state,
historyNavgation.location
);
const routerHistory = Object.assign(
{},
historyNavgation,
historyListeners
);
Object.defineProperty(routerHistory, "location", {
get: () => historyNavgation.location.value,
});
Object.defineProperty(routerHistory, "state", {
get: () => historyNavgation.state.value,
});
return routerHistory;
}
function createWebHashHistory() {
return createWebHistory("#");
}
// history 模式路由系统
// const routerHistory = createWebHistory();
// hash 模式路由系统
const routerHistory = createWebHashHistory();
routerHistory.listen((to, from, { isBack }) => {
console.log(
"to:",
to,
" from:",
from,
" 是后退吗?",
isBack
);
});
</script>
</body>
</html>