Skip to content
This repository was archived by the owner on Aug 18, 2020. It is now read-only.

Commit 291803b

Browse files
committed
feat: support browserHistory
1 parent 7b3eb83 commit 291803b

File tree

5 files changed

+43
-25
lines changed

5 files changed

+43
-25
lines changed

example/app.html

+19-15
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,27 @@
2020
})
2121

2222
const router = new SvelteRouter({
23-
'/': Home,
24-
'/welcome': {
25-
Component: Welcome,
26-
props: {
27-
store
28-
}
29-
},
30-
'/test/replace': Replace,
31-
'/hello/:name': {
32-
Component: Hello,
33-
props: {
34-
data: {
35-
salutation: 'Hello'
23+
mode: 'hash',
24+
// mode: 'history',
25+
routes: {
26+
'/': Home,
27+
'/welcome': {
28+
Component: Welcome,
29+
props: {
30+
store
3631
}
3732
},
38-
},
39-
default: NotFound
33+
'/test/replace': Replace,
34+
'/hello/:name': {
35+
Component: Hello,
36+
props: {
37+
data: {
38+
salutation: 'Hello'
39+
}
40+
},
41+
},
42+
default: NotFound
43+
}
4044
})
4145
SvelteRouter.listen(() => {
4246
console.log('router changed')

rollup.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ if (process.env.NODE_ENV === 'production') {
4343
config.input = './example/main.js'
4444
config.plugins.unshift(
4545
serve({
46+
historyApiFallback: true,
4647
contentBase: ['lib', 'build']
4748
}),
4849
livereload('release')

src/components/RouterLink.html

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
</a>
44

55
<script>
6-
import history from '../history'
76
const activedClassName = 'router-link-active'
87

98
export default {
109
data () {
1110
return {
1211
replace: false,
1312
to: '/',
14-
basePath: '#',
13+
basePath: '',
1514
active: false,
1615
class: '',
1716
activeClass: activedClassName,

src/history.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1-
import createHistory from 'history/createHashHistory'
1+
import createBrowserHistory from 'history/es/createBrowserHistory'
2+
import createHashHistory from 'history/es/createHashHistory'
23

3-
const history = createHistory()
4+
const history = mode => {
5+
switch (mode) {
6+
case 'history':
7+
return createBrowserHistory()
8+
default:
9+
return createHashHistory()
10+
}
11+
}
412

513
export default history

src/router.js

+12-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import history from './history'
1+
import createHistory from './history'
22
import {
33
getPathRegex,
44
getPathVariables,
@@ -7,6 +7,8 @@ import {
77

88
const DEFAULT_ROUTE = 'default'
99

10+
let history
11+
1012
class SvelteRouter {
1113
target = null
1214
listener = null
@@ -15,6 +17,10 @@ class SvelteRouter {
1517

1618
constructor (options) {
1719
this.options = options
20+
history = createHistory(options.mode)
21+
Object.defineProperty(window, 'history', {
22+
get: () => history
23+
})
1824
}
1925

2026
create (target) {
@@ -40,21 +46,21 @@ class SvelteRouter {
4046
this.content = null
4147
}
4248

43-
for (let path in this.options) {
44-
if (Object.prototype.hasOwnProperty.call(this.options, path)) {
49+
for (let path in this.options.routes) {
50+
if (Object.prototype.hasOwnProperty.call(this.options.routes, path)) {
4551
const sections = path.split('/')
4652
const regexPath = getPathRegex(sections)
4753
const matches = location.pathname.match(new RegExp(`^${regexPath}$`))
4854
if (matches !== null) {
4955
const pathVariables = getPathVariables(sections, matches)
50-
this.content = getContent(this.options, path, this.target, pathVariables)
56+
this.content = getContent(this.options.routes, path, this.target, pathVariables)
5157
found = true
5258
break
5359
}
5460
}
5561
}
56-
if (!found && this.options[DEFAULT_ROUTE]) {
57-
this.content = getContent(this.options, DEFAULT_ROUTE, this.target, {})
62+
if (!found && this.options.routes[DEFAULT_ROUTE]) {
63+
this.content = getContent(this.options.routes, DEFAULT_ROUTE, this.target, {})
5864
}
5965
}
6066
}

0 commit comments

Comments
 (0)