Skip to content

Commit b6aa7da

Browse files
committed
use router.map()
1 parent 961743c commit b6aa7da

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
lines changed

example/example.js

+34-35
Original file line numberDiff line numberDiff line change
@@ -49,46 +49,45 @@ var root = new Vue({
4949
}
5050
})
5151

52-
router.on('/inbox', {
53-
name: 'inbox',
54-
component: 'inbox',
55-
subRoutes: {
56-
'/message/:messageId': {
57-
name: 'message',
58-
component: 'message',
59-
alwaysRefresh: true
60-
},
61-
'/archived': {
62-
name: 'archive',
63-
component: 'archive'
52+
router.map({
53+
'/inbox': {
54+
name: 'inbox',
55+
component: 'inbox',
56+
subRoutes: {
57+
'/message/:messageId': {
58+
name: 'message',
59+
component: 'message',
60+
alwaysRefresh: true
61+
},
62+
'/archived': {
63+
name: 'archive',
64+
component: 'archive'
65+
}
6466
}
65-
}
66-
})
67-
68-
router.on('/user/:userId', {
69-
name: 'user',
70-
component: 'user',
71-
subRoutes: {
72-
'profile/:something': {
73-
component: 'user-profile'
74-
},
75-
'posts': {
76-
component: 'user-posts'
77-
},
78-
'settings': {
79-
component: 'user-settings'
67+
},
68+
'/user/:userId': {
69+
name: 'user',
70+
component: 'user',
71+
subRoutes: {
72+
'profile/:something': {
73+
component: 'user-profile'
74+
},
75+
'posts': {
76+
component: 'user-posts'
77+
},
78+
'settings': {
79+
component: 'user-settings'
80+
}
8081
}
82+
},
83+
'/about': {
84+
component: 'about'
85+
},
86+
'*': {
87+
component: 'not-found'
8188
}
8289
})
8390

84-
router.on('/about', {
85-
component: 'about',
86-
})
87-
88-
router.notfound({
89-
component: 'not-found'
90-
})
91-
9291
router.redirect({
9392
'/info': '/about'
9493
})

src/index.js

+17-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,20 @@ var p = VueRouter.prototype
3535
//
3636
// Public API
3737
//
38+
//
3839

3940
/**
40-
* Register a root-level path
41+
* Register a map of top-level paths.
42+
*/
43+
44+
p.map = function (map) {
45+
for (var route in map) {
46+
this.on(route, map[route])
47+
}
48+
}
49+
50+
/**
51+
* Register a single root-level path
4152
*
4253
* @param {String} rootPath
4354
* @param {Object} config
@@ -49,7 +60,11 @@ var p = VueRouter.prototype
4960
*/
5061

5162
p.on = function (rootPath, config) {
52-
this._addRoute(rootPath, config, [])
63+
if (rootPath === '*') {
64+
this.notfound(config)
65+
} else {
66+
this._addRoute(rootPath, config, [])
67+
}
5368
}
5469

5570
/**

0 commit comments

Comments
 (0)