|
| 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 | +} |
0 commit comments