Skip to content

Commit 7e7a398

Browse files
Domino9697posva
authored andcommitted
fix(ActiveLink): Fix active links when parent link redirects to child
Fix active parent link when parent link redirects to a child and when the user navigates between child links fix vuejs#2724
1 parent da1a114 commit 7e7a398

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

examples/active-links/app.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,17 @@ const Users = {
1717

1818
const User = { template: '<div>{{ $route.params.username }}</div>' }
1919

20+
const Gallery = {
21+
template: `
22+
<div>
23+
<h2>Gallery</h2>
24+
<router-view></router-view>
25+
</div>
26+
`
27+
}
28+
29+
const Image = { template: '<div>{{ $route.params.imageId }}</div>' }
30+
2031
const router = new VueRouter({
2132
mode: 'history',
2233
base: __dirname,
@@ -27,7 +38,11 @@ const router = new VueRouter({
2738
children: [
2839
{ path: ':username', name: 'user', component: User }
2940
]
30-
}
41+
},
42+
{ path: '/gallery', component: Gallery, name: 'gallery',
43+
children: [
44+
{ path: ':imageId', component: Image, name: 'image' }
45+
], redirect: { name: 'image', params: { imageId: 'image1' }}}
3146
]
3247
})
3348

@@ -66,6 +81,10 @@ new Vue({
6681
<router-link tag="li" to="/about">
6782
<a>/about (active class on outer element)</a>
6883
</router-link>
84+
85+
<li><router-link :to="{ name: 'gallery' }">/gallery (redirect to /gallery/image1)</router-link></li>
86+
<li><router-link :to="{ name: 'image', params: {imageId: 'image2'} }">/gallery/image2</router-link></li>
87+
<li><router-link :to="{ name: 'image', params: {imageId: 'image1'} }">/gallery/image1</router-link></li>
6988
</ul>
7089
<router-view class="view"></router-view>
7190
</div>

src/components/link.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { createRoute, isSameRoute, isIncludedRoute } from '../util/route'
44
import { extend } from '../util/misc'
5+
import { normalizeLocation } from '../util/location'
56

67
// work around weird flow bug
78
const toTypes: Array<Function> = [String, Object]
@@ -49,8 +50,8 @@ export default {
4950
const exactActiveClass = this.exactActiveClass == null
5051
? exactActiveClassFallback
5152
: this.exactActiveClass
52-
const compareTarget = location.path
53-
? createRoute(null, location, null, router)
53+
const compareTarget = route.redirectedFrom
54+
? createRoute(null, normalizeLocation(route.redirectedFrom), null, router)
5455
: route
5556

5657
classes[exactActiveClass] = isSameRoute(current, compareTarget)

test/e2e/specs/active-links.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module.exports = {
99
browser
1010
.url('http://localhost:8080/active-links/')
1111
.waitForElementVisible('#app', 1000)
12-
.assert.count('li a', 11)
12+
.assert.count('li a', 14)
1313
// assert correct href with base
1414
.assert.attributeContains('li:nth-child(1) a', 'href', '/active-links/')
1515
.assert.attributeContains('li:nth-child(2) a', 'href', '/active-links/')
@@ -22,6 +22,9 @@ module.exports = {
2222
.assert.attributeContains('li:nth-child(9) a', 'href', '/active-links/users/evan?foo=bar&baz=qux')
2323
.assert.attributeContains('li:nth-child(10) a', 'href', '/active-links/about')
2424
.assert.attributeContains('li:nth-child(11) a', 'href', '/active-links/about')
25+
.assert.attributeContains('li:nth-child(12) a', 'href', '/active-links/gallery')
26+
.assert.attributeContains('li:nth-child(13) a', 'href', '/active-links/gallery/image2')
27+
.assert.attributeContains('li:nth-child(14) a', 'href', '/active-links/gallery/image1')
2528
.assert.containsText('.view', 'Home')
2629

2730
assertActiveLinks(1, [1, 2], null, [1, 2])
@@ -35,6 +38,9 @@ module.exports = {
3538
assertActiveLinks(9, [1, 3, 5, 7, 9], null, [9])
3639
assertActiveLinks(10, [1, 10], [11], [10], [11])
3740
assertActiveLinks(11, [1, 10], [11], [10], [11])
41+
assertActiveLinks(12, [1, 12, 14], null, [14])
42+
assertActiveLinks(13, [1, 12, 13], null, [13])
43+
assertActiveLinks(14, [1, 12, 14], null, [14])
3844

3945
browser.end()
4046

0 commit comments

Comments
 (0)