Skip to content

Commit a3ab379

Browse files
laysentQingWei-Li
authored andcommitted
fix: incorrect active link (#281)
* fix scroll issue in IE * add meta tag for IE browser * fix link render issue after page refreshing * fix issue of incorrect active link Should use both `path` and `id` to identify link, as `id` might not be unique under different paths
1 parent 6949455 commit a3ab379

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

src/core/event/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { scrollIntoView } from './scroll'
55

66
export function eventMixin (proto) {
77
proto.$resetEvents = function () {
8-
scrollIntoView(this.route.query.id)
8+
scrollIntoView(this.route.path, this.route.query.id)
99
sidebar.getAndActive(this.router, 'nav')
1010
}
1111
}

src/core/event/scroll.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ function highlight () {
6868
}
6969
}
7070

71+
function getNavKey (path, id) {
72+
return `${path}?id=${id}`
73+
}
74+
7175
export function scrollActiveSidebar (router) {
7276
const cover = dom.find('.cover.show')
7377
coverHeight = cover ? cover.offsetHeight : 0
@@ -82,7 +86,8 @@ export function scrollActiveSidebar (router) {
8286
let href = a.getAttribute('href')
8387

8488
if (href !== '/') {
85-
href = router.parse(href).query.id
89+
const { query: { id }, path } = router.parse(href)
90+
if (id) href = getNavKey(path, id)
8691
}
8792

8893
if (href) nav[decodeURIComponent(href)] = li
@@ -100,13 +105,13 @@ export function scrollActiveSidebar (router) {
100105
})
101106
}
102107

103-
export function scrollIntoView (id) {
108+
export function scrollIntoView (path, id) {
104109
if (!id) return
105110

106111
const section = dom.find('#' + id)
107112
section && scrollTo(section)
108113

109-
const li = nav[id]
114+
const li = nav[getNavKey(path, id)]
110115
const sidebar = dom.getNode('.sidebar')
111116
const active = dom.find(sidebar, 'li.active')
112117
active && active.classList.remove('active')

src/core/router/history/hash.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class HashHistory extends History {
5555
let query = ''
5656

5757
const hashIndex = path.indexOf('#')
58-
if (hashIndex) {
58+
if (hashIndex >= 0) {
5959
path = path.slice(hashIndex + 1)
6060
}
6161

0 commit comments

Comments
 (0)