|
1 | 1 | ---
|
2 | 2 | title: Migration from Vue Router 0.7.x
|
3 | 3 | type: guide
|
4 |
| -order: 25 |
| 4 | +order: 26 |
5 | 5 | ---
|
6 | 6 |
|
7 | 7 | > Only Vue Router 2 is compatible with Vue 2, so if you're updating Vue, you'll have to update Vue Router as well. That's why we've included details on the migration path here in the main docs. For a complete guide on using the new Vue Router, see the [Vue Router docs](http://router.vuejs.org/en/).
|
8 | 8 |
|
9 |
| -<p class="tip">The list of deprecations below should be relatively complete, but the migration helper is still being updated to catch them.</p> |
| 9 | +## Router Initialization |
| 10 | + |
| 11 | +### `router.start` <sup>deprecated</sup> |
| 12 | + |
| 13 | +There is no longer a special API to initialize an app with Vue Router. That means instead of: |
| 14 | + |
| 15 | +``` js |
| 16 | +router.start({ |
| 17 | + template: '<router-view></router-view>' |
| 18 | +}, '#app') |
| 19 | +``` |
| 20 | + |
| 21 | +You'll just pass a router property to a Vue instance: |
| 22 | + |
| 23 | +``` js |
| 24 | +new Vue({ |
| 25 | + el: '#app', |
| 26 | + router: router, |
| 27 | + template: '<router-view></router-view>' |
| 28 | +}) |
| 29 | +``` |
| 30 | + |
| 31 | +Or, if you're using the runtime-only build of Vue: |
| 32 | + |
| 33 | +``` js |
| 34 | +new Vue({ |
| 35 | + el: '#app', |
| 36 | + router: router, |
| 37 | + render: h => h('router-view') |
| 38 | +}) |
| 39 | +``` |
| 40 | + |
| 41 | +{% raw %} |
| 42 | +<div class="upgrade-path"> |
| 43 | + <h4>Upgrade Path</h4> |
| 44 | + <p>Run the <a href="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of <code>router.start</code> being called.</p> |
| 45 | +</div> |
| 46 | +{% endraw %} |
10 | 47 |
|
11 | 48 | ## Route Definitions
|
12 | 49 |
|
@@ -91,14 +128,14 @@ router.match = createMatcher(
|
91 | 128 | </div>
|
92 | 129 | {% endraw %}
|
93 | 130 |
|
94 |
| -### `subroutes` <sup>deprecated</sup> |
| 131 | +### `subRoutes` <sup>deprecated</sup> |
95 | 132 |
|
96 | 133 | [Renamed to `children`](http://router.vuejs.org/en/essentials/nested-routes.html) for consistency within Vue and with other routing libraries.
|
97 | 134 |
|
98 | 135 | {% raw %}
|
99 | 136 | <div class="upgrade-path">
|
100 | 137 | <h4>Upgrade Path</h4>
|
101 |
| - <p>Run the <a href="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of the <code>subroutes</code> option.</p> |
| 138 | + <p>Run the <a href="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of the <code>subRoutes</code> option.</p> |
102 | 139 | </div>
|
103 | 140 | {% endraw %}
|
104 | 141 |
|
@@ -161,6 +198,44 @@ alias: ['/manage', '/administer', '/administrate']
|
161 | 198 | </div>
|
162 | 199 | {% endraw %}
|
163 | 200 |
|
| 201 | +### Arbitrary Route Properties |
| 202 | + |
| 203 | +Arbitrary route properties must now be scoped under the new meta property, to avoid conflicts with future features. So for example, if you had defined: |
| 204 | + |
| 205 | +``` js |
| 206 | +'/admin': { |
| 207 | + component: AdminPanel, |
| 208 | + requiresAuth: true |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +Then you would now update it to: |
| 213 | + |
| 214 | +``` js |
| 215 | +{ |
| 216 | + path: '/admin', |
| 217 | + component: AdminPanel, |
| 218 | + meta: { |
| 219 | + requiresAuth: true |
| 220 | + } |
| 221 | +} |
| 222 | +``` |
| 223 | + |
| 224 | +Then when later accessing this property on a route, you will still go through meta. For example: |
| 225 | + |
| 226 | +``` js |
| 227 | +if (route.meta.requiresAuth) { |
| 228 | + // ... |
| 229 | +} |
| 230 | +``` |
| 231 | + |
| 232 | +{% raw %} |
| 233 | +<div class="upgrade-path"> |
| 234 | + <h4>Upgrade Path</h4> |
| 235 | + <p>Run the <a href="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of arbitrary route properties not scoped under meta.</p> |
| 236 | +</div> |
| 237 | +{% endraw %} |
| 238 | + |
164 | 239 | ## Route Matching
|
165 | 240 |
|
166 | 241 | Route matching now uses [path-to-regexp](https://github.com/pillarjs/path-to-regexp) under the hood, making it much more flexible than previously.
|
@@ -230,12 +305,12 @@ The `<a>` will be the actual link (and will get the correct href), but the activ
|
230 | 305 |
|
231 | 306 | ### `router.go`
|
232 | 307 |
|
233 |
| -[Renamed to `router.push`](http://router.vuejs.org/en/essentials/navigation.html#routerpushlocation) for consistency with terminology used in the [HTML5 History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API). |
| 308 | +For consistency with the [HTML5 History API](https://developer.mozilla.org/en-US/docs/Web/API/History_API), `router.go` is now only used for [back/forward navigation](https://router.vuejs.org/en/essentials/navigation.html#routergon), while [`router.push`](http://router.vuejs.org/en/essentials/navigation.html#routerpushlocation) is used to navigate to a specific page. |
234 | 309 |
|
235 | 310 | {% raw %}
|
236 | 311 | <div class="upgrade-path">
|
237 | 312 | <h4>Upgrade Path</h4>
|
238 |
| - <p>Run the <a href="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of <code>router.go</code> being called.</p> |
| 313 | + <p>Run the <a href="https://github.com/vuejs/vue-migration-helper">migration helper</a> on your codebase to find examples of <code>router.go</code> being used where <code>router.push</code> should be used instead.</p> |
239 | 314 | </div>
|
240 | 315 | {% endraw %}
|
241 | 316 |
|
@@ -453,20 +528,22 @@ data: function () {
|
453 | 528 | },
|
454 | 529 | watch: {
|
455 | 530 | '$route': function () {
|
456 |
| - this.isLoading = true |
457 |
| - this.fetchData().then(() => { |
458 |
| - this.isLoading = false |
| 531 | + var self = this |
| 532 | + self.isLoading = true |
| 533 | + self.fetchData().then(function () { |
| 534 | + self.isLoading = false |
459 | 535 | })
|
460 | 536 | }
|
461 | 537 | },
|
462 | 538 | methods: {
|
463 | 539 | fetchData: function () {
|
| 540 | + var self = this |
464 | 541 | return axios.get('/api/posts')
|
465 | 542 | .then(function (response) {
|
466 |
| - this.posts = response.data.posts |
| 543 | + self.posts = response.data.posts |
467 | 544 | })
|
468 | 545 | .catch(function (error) {
|
469 |
| - this.fetchError = error |
| 546 | + self.fetchError = error |
470 | 547 | })
|
471 | 548 | }
|
472 | 549 | }
|
|
0 commit comments