v2.0.0-beta.1
Pre-release[email protected] only works with vue@^2.0.0-beta.4
This is a major release with numerous breaking changes, the following describes some most important changes from 0.7.x, but may not be fully exhaustive.
Generic API Change
- The old
router.go()
is nowrouter.push()
. - The new
router.go
is similar towindow.history.go()
: it accepts a number for navigating in the history stack. - New methods:
router.back()
router.forward()
Router Configuration
All router configurations are now passed to the Router constructor in a single object. For available options, see the configuration object's type declaration.
The routes
option replaces router.map()
. In addition, route configuration now uses Arrays instead of Object hashes. This ensures consistent route matching precedence. (Object key enumeration order is browser-dependent)
See here for an example of the new configuration syntax.
The following router instantiation options are deprecated:
history
(replaced bymode
)abstract
(replaced bymode
)root
(replaced bybase
)saveScrollPosition
(replaced byscrollBehavior
with much more flexibility, see below)hashbang
(removed since the hashbang is no longer required for Google to crawl the URL)transitionOnLoad
(removed because Vue 2.0 has explicitappear
transition control)suppressTransitionError
(removed due to hooks simplification)
The new mode
option should be one of the following (defaults to "hash"
):
"abstract"
"hash"
"history"
In browsers that do not support history.pushState
, the router will automatically fallback to hash mode.
The following methods are deprecated:
router.map
(replaced by theroutes
option)router.beforeEach
(replaced by thebeforeEach
option)router.afterEach
(replaced by theafterEach
option)router.redirect
(now declared inroutes
, see Example)router.alias
(now declared inroutes
, see Example)
Navigation Hooks
The hooks system has been drastically simplified. All 0.7 transition hooks are deprecated. Here's how to achieve similar functionalities:
- Use component's own lifecycle hooks to replace
activate
anddeactivate
. - Use a watcher on
$route
to react to route changes (e.g. fetch new data based on new route params - Example) canActivate
is replaced bybeforeEnter
guards declared in route configurations. ExamplecanDeactivate
is replaced bybeforeRouteLeave
, which is defined at the root level of a component's definition. This hook is called with the component instance as itsthis
context. ExamplecanReuse
is removed because it is confusing and rarely useful.
In addition, all route hook functions in 2.0 share the same simplified signature:
guard (toRoute, redirect, next) {
// call redirect to redirect to another route
// call next to confirm current route
// or do nothing to cancel the navigation
}
They no longer support returning Promises.
Links
The v-link
directive has been replaced by the <router-link>
component. The component accepts the following props:
to
: a string path, or an object location descriptor.tag
: the element to render. Defaults toa
.exact
: for active class matching behavior.append
: for relative link appending behaviorreplace
: replace instead of push history entryactiveClass
: the CSS class to add when the link is active
See a comprehensive example of <router-link>
usage.
Named Views
A single route can now map to multiple named components. These components will be rendered into multiple <router-view>
s with corresponding names. Example
Scroll Behavior
The scrollBehavior
option expects a function that returns instructions on how the page should scroll on route navigations. You can programmatically determine whether to scroll to top, scroll to hash anchor, or scroll to the saved position from popstate. Example