Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in
Toggle navigation
V
vue-router
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
shangbj
vue-router
Commits
01918096
Commit
01918096
authored
Mar 13, 2021
by
尚斌杰
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(hash):增加hash路由
parent
121a1256
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
8 deletions
+21
-8
index.js
src/router/index.js
+1
-1
index.js
src/vueRouter/index.js
+20
-7
No files found.
src/router/index.js
View file @
01918096
...
...
@@ -21,7 +21,7 @@ const routes = [
]
const
router
=
new
VueRouter
({
mode
:
'history'
,
//
mode: 'history',
base
:
process
.
env
.
BASE_URL
,
routes
})
...
...
src/vueRouter/index.js
View file @
01918096
...
...
@@ -9,7 +9,6 @@ export default class VueRouter {
// 把Vue构造函数记录到全局变量
_Vue
=
Vue
// 把创建Vue实例时候传入的router对象注入到vue实例中
//
// 混入
_Vue
.
mixin
({
beforeCreate
()
{
...
...
@@ -25,11 +24,14 @@ export default class VueRouter {
this
.
options
=
options
this
.
routeMap
=
{}
this
.
data
=
_Vue
.
observable
({
current
:
window
.
location
.
pathname
current
:
window
.
location
.
pathname
})
}
init
(){
if
(
!
(
this
.
options
.
mode
&&
this
.
options
.
mode
===
'history'
)
&&
!
window
.
location
.
hash
){
window
.
location
.
hash
=
'#/'
}
this
.
createRouteMap
()
this
.
initComponents
(
_Vue
)
this
.
initEvent
()
...
...
@@ -43,6 +45,7 @@ export default class VueRouter {
}
initComponents
(
Vue
){
const
self
=
this
Vue
.
component
(
'router-link'
,
{
props
:{
to
:
String
...
...
@@ -60,14 +63,17 @@ export default class VueRouter {
},
methods
:{
clickHandler
(
e
){
history
.
pushState
({},
''
,
this
.
to
)
if
(
self
.
options
.
mode
&&
self
.
options
.
mode
===
'history'
){
history
.
pushState
({},
''
,
this
.
to
)
}
else
{
location
.
hash
=
`#
${
this
.
to
}
`
}
this
.
$router
.
data
.
current
=
this
.
to
e
.
preventDefault
();
}
}
})
const
self
=
this
Vue
.
component
(
'router-view'
,
{
render
(
h
){
const
component
=
self
.
routeMap
[
self
.
data
.
current
]
...
...
@@ -77,8 +83,15 @@ export default class VueRouter {
}
initEvent
(){
window
.
addEventListener
(
'popstate'
,()
=>
{
this
.
data
.
current
=
window
.
location
.
pathname
})
if
(
this
.
options
.
mode
&&
this
.
options
.
mode
===
'history'
){
window
.
addEventListener
(
'popstate'
,()
=>
{
this
.
data
.
current
=
window
.
location
.
pathname
})
}
else
{
window
.
addEventListener
(
'hashchange'
,()
=>
{
this
.
data
.
current
=
window
.
location
.
hash
.
replace
(
'#'
,
''
)
})
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment