|
| 1 | +# Vue-router 2.x integration for Meteor |
| 2 | + |
| 3 | +## Add routes in the blink of an eye. |
| 4 | +Routing for vue and meteor using [vue-router](https://github.com/vuejs/vue-router). |
| 5 | + |
| 6 | +See the [example here](https://github.com/Akryum/meteor-vue2-example-routing). |
| 7 | + |
| 8 | +## Installation |
| 9 | + |
| 10 | + |
| 11 | + meteor add akryum:vue-router2 |
| 12 | + |
| 13 | +## Usage |
| 14 | + |
| 15 | +### Router options |
| 16 | + |
| 17 | +First, let's create our router: |
| 18 | + |
| 19 | +```javascript |
| 20 | +/* /client/client.js */ |
| 21 | + |
| 22 | +// Import the router |
| 23 | +import {Router, nativeScrollBehavior} from 'meteor/akryum:vue-router2'; |
| 24 | + |
| 25 | +// Create router instance |
| 26 | +const router = new Router({ |
| 27 | + mode: 'history', |
| 28 | + scrollBehavior: nativeScrollBehavior, |
| 29 | +}); |
| 30 | +``` |
| 31 | + |
| 32 | +When you create the router instance, you can pass `options` that allow you to customize the router behavior ([more info](http://router.vuejs.org/en/api/options.html)). |
| 33 | + |
| 34 | +### Route definition |
| 35 | + |
| 36 | +In your client, add some routes with the `Router.configure()` method (for more info about route definition, check the [vue-router documentation](http://router.vuejs.org/en/essentials/nested-routes.html)): |
| 37 | + |
| 38 | +```javascript |
| 39 | +/* /client/routes.js */ |
| 40 | + |
| 41 | +// Import the router |
| 42 | +import {Router} from 'meteor/akryum:vue-router2'; |
| 43 | + |
| 44 | +// Components |
| 45 | +import Home from '/imports/ui/Home.vue'; |
| 46 | +import Forum from '/imports/ui/Forum.vue'; |
| 47 | +import Apollo from '/imports/ui/Apollo.vue'; |
| 48 | + |
| 49 | +Router.configure(router => { |
| 50 | + // Simple routes |
| 51 | + router.addRoutes([ |
| 52 | + { |
| 53 | + path: '/', |
| 54 | + name: 'home', |
| 55 | + component: Home, |
| 56 | + }, |
| 57 | + { |
| 58 | + path: '/forum', |
| 59 | + name: 'forum', |
| 60 | + component: Forum, |
| 61 | + }, |
| 62 | + { |
| 63 | + path: '/apollo', |
| 64 | + name: 'apollo', |
| 65 | + component: Apollo, |
| 66 | + }, |
| 67 | + ]); |
| 68 | +}); |
| 69 | +``` |
| 70 | + |
| 71 | +**Attention! The order of the routes matters during the route matching!*** |
| 72 | + |
| 73 | +The callbacks you pass to the `Router.configure()` calls will be called before the router is started, regardless of the file load order. |
| 74 | + |
| 75 | +You can pass a second integer argument `priority`, that changes the order in which the callbacks are called to add routes. Callbacks with higher priority will be called before the others. The default priority is `0`. |
| 76 | + |
| 77 | +#### Simple syntax |
| 78 | + |
| 79 | +You can use an alternative special syntax in `.routes.js` files: |
| 80 | + |
| 81 | +```javascript |
| 82 | +/* /client/main.routes.js */ |
| 83 | +export default [ |
| 84 | + { |
| 85 | + path: '/', |
| 86 | + name: 'home', |
| 87 | + component: '/imports/ui/Home.vue' |
| 88 | + }, |
| 89 | + { |
| 90 | + path: '/forum', |
| 91 | + name: 'forum', |
| 92 | + component: '/imports/ui/Forum.vue' |
| 93 | + }, |
| 94 | + { |
| 95 | + path: '/apollo', |
| 96 | + name: 'apollo', |
| 97 | + component: '/imports/ui/Apollo.vue' |
| 98 | + } |
| 99 | +]; |
| 100 | +``` |
| 101 | + |
| 102 | +All the routes will be automatically added and the component's paths resolved. |
| 103 | + |
| 104 | +### App menu |
| 105 | + |
| 106 | +Use the `router-link` component to add dynamic links that take to different routes in your app ([more info](http://router.vuejs.org/en/api/router-link.html)): |
| 107 | + |
| 108 | +```html |
| 109 | +<!-- /imports/ui/AppMenu.vue --> |
| 110 | +<template> |
| 111 | +<div class="app-menu"> |
| 112 | + <router-link :to="{ name:'home', exact: true }">Home</router-link> |
| 113 | + <router-link :to="{ name:'forum' }">Forum</router-link> |
| 114 | + <router-link :to="{ name:'apollo' }">Apollo</router-link> |
| 115 | +</div> |
| 116 | +</template> |
| 117 | + |
| 118 | +<style scoped lang="sass"> |
| 119 | +@import ~imports/ui/colors.sass |
| 120 | +
|
| 121 | +.app-menu |
| 122 | + margin: 32px 0 |
| 123 | + text-align: center |
| 124 | +
|
| 125 | + a |
| 126 | + display: inline-block |
| 127 | + padding: 6px |
| 128 | + margin: 0 6px |
| 129 | + border-radius: 3px |
| 130 | + &.v-link-active |
| 131 | + background: $app-color |
| 132 | + color: white |
| 133 | +</style> |
| 134 | +``` |
| 135 | +
|
| 136 | +### App layout |
| 137 | +
|
| 138 | +Create a vue component with a `<router-view></router-view>` element, that will contain the route content ([more info](http://router.vuejs.org/en/api/router-view.html)): |
| 139 | +
|
| 140 | +```html |
| 141 | +<!-- /imports/ui/AppLayout.vue --> |
| 142 | +<template> |
| 143 | +<div class="app-layout"> |
| 144 | + <!-- Menu --> |
| 145 | + <app-menu></app-menu> |
| 146 | +
|
| 147 | + <!-- Route content --> |
| 148 | + <router-view></router-view> |
| 149 | +</div> |
| 150 | +</template> |
| 151 | +
|
| 152 | +<script> |
| 153 | +import AppMenu from '/imports/ui/AppMenu.vue'; |
| 154 | +
|
| 155 | +export default { |
| 156 | + components: { |
| 157 | + AppMenu |
| 158 | + } |
| 159 | +} |
| 160 | +</script> |
| 161 | +``` |
| 162 | +
|
| 163 | +### Not found page |
| 164 | +
|
| 165 | +To add a 'not found' page, add a `*` route in your client code: |
| 166 | +
|
| 167 | +```javascript |
| 168 | +/* /client/routes.js */ |
| 169 | +
|
| 170 | +// Import the router |
| 171 | +import {Router} from 'meteor/akryum:vue-router'; |
| 172 | +
|
| 173 | +// Not found |
| 174 | +import NotFound from '/imports/ui/NotFound.vue'; |
| 175 | +
|
| 176 | +Router.configure(router => { |
| 177 | + router.addRoute({ |
| 178 | + path: '*', |
| 179 | + component: NotFound, |
| 180 | + }); |
| 181 | +}, -1); |
| 182 | +``` |
| 183 | +
|
| 184 | +*Note that we use a priority of `-1`, so this route is added last. If we don't do that, there is a chance that this route will be first and then will match immediatly: the user will be greeted by a 'not found' page everytime he loads the app!* |
| 185 | +
|
| 186 | +### Starting the router |
| 187 | +
|
| 188 | +Then import the routes and start the router in your client: |
| 189 | +
|
| 190 | +```javascript |
| 191 | +/* /client/client.js */ |
| 192 | +
|
| 193 | +// App layout |
| 194 | +import AppLayout from '/imports/ui/AppLayout.vue'; |
| 195 | +
|
| 196 | +// App start |
| 197 | +Meteor.startup(() => { |
| 198 | + // Start the router |
| 199 | + new Vue({ |
| 200 | + router: router.start(), |
| 201 | + render: h => h(AppLayout), |
| 202 | + }).$mount('app'); |
| 203 | +}); |
| 204 | +
|
| 205 | +``` |
| 206 | +
|
| 207 | +**If you put your routes files in the `/imports` folder, you need to import them manually.** |
| 208 | +
|
| 209 | +To start the router, use the `router.start()` method and pass the result to a Vue instance with the `router` option. |
| 210 | +
|
| 211 | +### Fast-render |
| 212 | +
|
| 213 | +You can use the [meteorhacks:fast-render](https://github.com/kadirahq/fast-render) package to inject the subscriptions data in the html. This greatly speeds up the initial render of your app if it depends on subscriptions. |
| 214 | +
|
| 215 | +First, install the fast-render package: |
| 216 | +
|
| 217 | + meteor add meteorhacks:fast-render |
| 218 | +
|
| 219 | +In your server, add fast-render routes: |
| 220 | +
|
| 221 | +```javascript |
| 222 | +FastRender.route('/forum', function(params) { |
| 223 | + this.subscribe('threads'); |
| 224 | +}); |
| 225 | +``` |
| 226 | +
|
| 227 | +This will send the `threads` subscription data along side the html if the user open your app with the `yourapp/forum` url. |
| 228 | +
|
| 229 | +--- |
| 230 | +
|
| 231 | +## Next steps |
| 232 | +
|
| 233 | +- [Example project](https://github.com/Akryum/meteor-vue2-example-routing) |
| 234 | +- [Integrate apollo](https://github.com/Akryum/vue-apollo) |
| 235 | +
|
| 236 | +--- |
| 237 | +
|
| 238 | +LICENCE ISC - Created by Guillaume CHAU (@Akryum) |
0 commit comments