Skip to content

Commit cd2f0df

Browse files
LinusBorgfnlctrl
authored andcommitted
router link now sets a hash path to href when using hash-mode. (#809)
1 parent 8188304 commit cd2f0df

File tree

5 files changed

+90
-1
lines changed

5 files changed

+90
-1
lines changed

examples/hash-mode/app.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Vue from 'vue'
2+
import VueRouter from 'vue-router'
3+
4+
// 1. Use plugin.
5+
// This installs <router-view> and <router-link>,
6+
// and injects $router and $route to all router-enabled child components
7+
Vue.use(VueRouter)
8+
9+
// 2. Define route components
10+
const Home = { template: '<div>home</div>' }
11+
const Foo = { template: '<div>foo</div>' }
12+
const Bar = { template: '<div>bar</div>' }
13+
14+
// 3. Create the router
15+
const router = new VueRouter({
16+
mode: 'hash',
17+
base: __dirname,
18+
routes: [
19+
{ path: '/', component: Home }, // all paths are defined without the hash.
20+
{ path: '/foo', component: Foo },
21+
{ path: '/bar', component: Bar }
22+
]
23+
})
24+
25+
// 4. Create and mount root instance.
26+
// Make sure to inject the router.
27+
// Route components will be rendered inside <router-view>.
28+
new Vue({
29+
router,
30+
template: `
31+
<div id="app">
32+
<h1>Basic</h1>
33+
<ul>
34+
<li><router-link to="/">/</router-link></li>
35+
<li><router-link to="/foo">/foo</router-link></li>
36+
<li><router-link to="/bar">/bar</router-link></li>
37+
<router-link tag="li" to="/bar">/bar</router-link>
38+
</ul>
39+
<router-view class="view"></router-view>
40+
</div>
41+
`
42+
}).$mount('#app')

examples/hash-mode/index.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<!DOCTYPE html>
2+
<link rel="stylesheet" href="/global.css">
3+
<a href="/">&larr; Examples index</a>
4+
<div id="app"></div>
5+
<script src="/__build__/shared.js"></script>
6+
<script src="/__build__/hash-mode.js"></script>

examples/index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<h1>Vue Router Examples</h1>
99
<ul>
1010
<li><a href="basic">Basic</a></li>
11+
<li><a href="hash-mode">Mode: 'hash'</a></li>
1112
<li><a href="nested-routes">Nested Routes</a></li>
1213
<li><a href="named-routes">Named Routes</a></li>
1314
<li><a href="named-views">Named Views</a></li>

src/components/link.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export default {
3030
const resolved = router.match(to)
3131
const fullPath = resolved.redirectedFrom || resolved.fullPath
3232
const base = router.history.base
33-
const href = base ? cleanPath(base + fullPath) : fullPath
33+
const href = createHref(base, fullPath, router.mode)
3434
const classes = {}
3535
const activeClass = this.activeClass || router.options.linkActiveClass || 'router-link-active'
3636
const compareTarget = to.path ? createRoute(null, to) : resolved
@@ -97,3 +97,8 @@ function findAnchor (children) {
9797
}
9898
}
9999
}
100+
101+
function createHref (base, fullPath, mode) {
102+
var path = mode === 'hash' ? '/#' + fullPath : fullPath
103+
return base ? cleanPath(base + path) : path
104+
}

test/e2e/specs/hash-mode.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
module.exports = {
2+
'Hash mode': function (browser) {
3+
browser
4+
.url('http://localhost:8080/hash-mode/')
5+
.waitForElementVisible('#app', 1000)
6+
.assert.count('li', 4)
7+
.assert.count('li a', 3)
8+
.assert.attributeContains('li:nth-child(1) a', 'href', '/hash-mode/#/')
9+
.assert.attributeContains('li:nth-child(2) a', 'href', '/hash-mode/#/foo')
10+
.assert.attributeContains('li:nth-child(3) a', 'href', '/hash-mode/#/bar')
11+
.assert.containsText('.view', 'home')
12+
13+
.click('li:nth-child(2) a')
14+
.assert.urlEquals('http://localhost:8080/hash-mode/#/foo')
15+
.assert.containsText('.view', 'foo')
16+
17+
.click('li:nth-child(3) a')
18+
.assert.urlEquals('http://localhost:8080/hash-mode/#/bar')
19+
.assert.containsText('.view', 'bar')
20+
21+
.click('li:nth-child(1) a')
22+
.assert.urlEquals('http://localhost:8080/hash-mode/#/')
23+
.assert.containsText('.view', 'home')
24+
25+
.click('li:nth-child(4)')
26+
.assert.urlEquals('http://localhost:8080/hash-mode/#/bar')
27+
.assert.containsText('.view', 'bar')
28+
29+
// check initial visit
30+
.url('http://localhost:8080/hash-mode/#/foo')
31+
.waitForElementVisible('#app', 1000)
32+
.assert.containsText('.view', 'foo')
33+
.end()
34+
}
35+
}

0 commit comments

Comments
 (0)