Skip to content

Commit 9ee1844

Browse files
author
Bojan Markovic
committed
Add support for 'disabled' attribute to <router-link>, fixes vuejs#916
1 parent 4a9470e commit 9ee1844

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

examples/navigation-guards/app.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@ const Quux = {
8787
}
8888
}
8989

90+
const Baf = {
91+
template: `
92+
<div>
93+
<p> baf, you should never see this</p>
94+
</div>
95+
`
96+
}
97+
9098
const router = new VueRouter({
9199
mode: 'history',
92100
base: __dirname,
@@ -106,15 +114,19 @@ const router = new VueRouter({
106114
// Qux implements an in-component beforeRouteEnter hook
107115
{ path: '/qux', component: Qux },
108116

109-
// in-component beforeRouteEnter hook for async components
117+
// in-component beforeRouteEnter hook for async components
110118
{ path: '/qux-async', component: resolve => {
111119
setTimeout(() => {
112120
resolve(Qux)
113121
}, 0)
114122
} },
115123

116124
// in-component beforeRouteUpdate hook
117-
{ path: '/quux/:id', component: Quux }
125+
{ path: '/quux/:id', component: Quux },
126+
127+
// Baf implements declarative "disabled" in template
128+
{ path: '/baf', component: Baf }
129+
118130
]
119131
})
120132

@@ -140,6 +152,7 @@ new Vue({
140152
<li><router-link to="/qux-async">/qux-async</router-link></li>
141153
<li><router-link to="/quux/1">/quux/1</router-link></li>
142154
<li><router-link to="/quux/2">/quux/2</router-link></li>
155+
<li><router-link to="/baf" disabled>/baf</router-link></li>
143156
</ul>
144157
<router-view class="view"></router-view>
145158
</div>

src/components/link.js

+5
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,11 @@ function guardEvent (e) {
119119
if (e.preventDefault) {
120120
e.preventDefault()
121121
}
122+
// if the currentTarget is disabled, preventDefault, don't redirect
123+
if (e.currentTarget && e.currentTarget.getAttribute('disabled')) {
124+
e.preventDefault && e.preventDefault()
125+
return
126+
}
122127
return true
123128
}
124129

test/e2e/specs/navigation-guards.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ module.exports = {
88
browser
99
.url('http://localhost:8080/navigation-guards/')
1010
.waitForElementVisible('#app', 1000)
11-
.assert.count('li a', 8)
11+
.assert.count('li a', 9)
1212
.assert.containsText('.view', 'home')
1313

14+
.click('li:nth-child(9) a')
15+
.waitFor(100)
16+
.assert.urlEquals('http://localhost:8080/navigation-guards/')
17+
.assert.containsText('.view', 'home')
18+
1419
.click('li:nth-child(2) a')
1520
.dismissAlert()
1621
.waitFor(100)

0 commit comments

Comments
 (0)