Skip to content

Commit 343dc4f

Browse files
committed
update example more
1 parent 14bd496 commit 343dc4f

File tree

3 files changed

+86
-82
lines changed

3 files changed

+86
-82
lines changed

example/index.js

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
var Vue = require('vue')
33
var VueRouter = require('../src')
44

5+
// install router
56
Vue.use(VueRouter)
67

78
// create router
@@ -10,32 +11,8 @@ var router = new VueRouter({
1011
saveScrollPosition: true
1112
})
1213

13-
// define routes
14-
router.map(require('./routes'))
15-
16-
// redirect
17-
router.redirect({
18-
'/info': '/about',
19-
'/hello/:userId': '/user/:userId'
20-
})
21-
22-
// global before
23-
// 3 options:
24-
// 1. return a boolean
25-
// 2. return a Promise that resolves to a boolean
26-
// 3. call transition.next() or transition.abort()
27-
router.beforeEach(function (transition) {
28-
if (transition.to.path === '/forbidden') {
29-
router.app.authenticating = true
30-
setTimeout(function () {
31-
router.app.authenticating = false
32-
alert('this route is forbidden by a global before hook')
33-
transition.abort()
34-
}, 500)
35-
} else {
36-
transition.next()
37-
}
38-
})
14+
// configure router
15+
require('./route-config')(router)
3916

4017
// boostrap the app
4118
var App = Vue.extend(require('./app.vue'))

example/route-config.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
module.exports = function (router) {
2+
3+
// normal routes
4+
router.map({
5+
// basic example
6+
'/about': {
7+
// the component can also be a plain string component id,
8+
// but a component with that id must be available in the
9+
// App component's scope.
10+
component: require('./components/about.vue')
11+
},
12+
13+
// nested example
14+
'/user/:userId': {
15+
component: require('./components/user/index.vue'),
16+
subRoutes: {
17+
// matches "/user/:userId/profile/:something"
18+
'profile/:something': {
19+
component: require('./components/user/profile.vue')
20+
},
21+
// matches "/user/:userId/posts"
22+
'posts': {
23+
component: require('./components/user/posts.vue')
24+
},
25+
// matches "/user/:userId/settings"
26+
'settings': {
27+
component: require('./components/user/settings.vue')
28+
}
29+
}
30+
},
31+
32+
// not found handler
33+
'*': {
34+
component: require('./components/not-found.vue')
35+
},
36+
37+
// advanced example
38+
'/inbox': {
39+
component: require('./components/inbox/index.vue'),
40+
subRoutes: {
41+
'/message/:messageId': {
42+
component: require('./components/inbox/message.vue')
43+
},
44+
'/archived': {
45+
component: require('./components/inbox/archive.vue')
46+
},
47+
// default component to render into the nested outlet
48+
// when the parent route is matched but there's no
49+
// nested segment. In this case, "/inbox".
50+
'*': {
51+
// inline component
52+
component: {
53+
template: 'default yo'
54+
}
55+
}
56+
}
57+
}
58+
})
59+
60+
// redirect
61+
router.redirect({
62+
'/info': '/about',
63+
'/hello/:userId': '/user/:userId'
64+
})
65+
66+
// global before
67+
// 3 options:
68+
// 1. return a boolean
69+
// 2. return a Promise that resolves to a boolean
70+
// 3. call transition.next() or transition.abort()
71+
router.beforeEach(function (transition) {
72+
if (transition.to.path === '/forbidden') {
73+
router.app.authenticating = true
74+
setTimeout(function () {
75+
router.app.authenticating = false
76+
alert('this route is forbidden by a global before hook')
77+
transition.abort()
78+
}, 500)
79+
} else {
80+
transition.next()
81+
}
82+
})
83+
}

example/routes.js

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)