Programmatic Navigation

Aside from using <router-link> to create anchor tags for declarative navigation, we can do this programmatically using the router’s instance methods.

Navigate to a different location

Note: Inside of a Vue instance, you have access to the router instance as $router. You can therefore call this.$router.push.

To navigate to a different URL, use router.push. This method pushes a new entry into the history stack, so when the user clicks the browser back button they will be taken to the previous URL.

This is the method called internally when you click a <router-link>, so clicking <router-link :to="..."> is the equivalent of calling router.push(...).

DeclarativeProgrammatic
<router-link :to=”…”>router.push(…)

The argument can be a string path, or a location descriptor object. Examples:

  1. // literal string path
  2. router.push('/users/eduardo')
  3. // object with path
  4. router.push({ path: '/users/eduardo' })
  5. // named route with params to let the router build the url
  6. router.push({ name: 'user', params: { username: 'eduardo' } })
  7. // with query, resulting in /register?plan=private
  8. router.push({ path: '/register', query: { plan: 'private' } })
  9. // with hash, resulting in /about#team
  10. router.push({ path: '/about', hash: '#team' })

Note: params are ignored if a path is provided, which is not the case for query, as shown in the example above. Instead, you need to provide the name of the route or manually specify the whole path with any parameter:

  1. const username = 'eduardo'
  2. // we can manually build the url but we will have to handle the encoding ourselves
  3. router.push(`/user/${username}`) // -> /user/eduardo
  4. // same as
  5. router.push({ path: `/user/${username}` }) // -> /user/eduardo
  6. // if possible use `name` and `params` to benefit from automatic URL encoding
  7. router.push({ name: 'user', params: { username } }) // -> /user/eduardo
  8. // `params` cannot be used alongside `path`
  9. router.push({ path: '/user', params: { username } }) // -> /user

When specifying params, make sure to either provide a string or number (or an array of these for repeatable params). Any other type (like undefined, false, etc) will be automatically stringified. For optional params, you can provide an empty string ("") as the value to skip it.

Since the prop to accepts the same kind of object as router.push, the exact same rules apply to both of them.

router.push and all the other navigation methods return a Promise that allows us to wait til the navigation is finished and to know if it succeeded or failed. We will talk more about that in Navigation Handling.

Replace current location

It acts like router.push, the only difference is that it navigates without pushing a new history entry, as its name suggests - it replaces the current entry.

DeclarativeProgrammatic
<router-link :to=”…” replace>router.replace(…)

It’s also possible to directly add a property replace: true to the routeLocation that is passed to router.push:

  1. router.push({ path: '/home', replace: true })
  2. // equivalent to
  3. router.replace({ path: '/home' })

Traverse history

This method takes a single integer as parameter that indicates by how many steps to go forward or go backward in the history stack, similar to window.history.go(n).

Examples

  1. // go forward by one record, the same as router.forward()
  2. router.go(1)
  3. // go back by one record, the same as router.back()
  4. router.go(-1)
  5. // go forward by 3 records
  6. router.go(3)
  7. // fails silently if there aren't that many records
  8. router.go(-100)
  9. router.go(100)

History Manipulation

You may have noticed that router.push, router.replace and router.go are counterparts of window.history.pushState, window.history.replaceState and window.history.go, and they do imitate the window.history APIs.

Therefore, if you are already familiar with Browser History APIs, manipulating history will feel familiar when using Vue Router.

It is worth mentioning that Vue Router navigation methods (push, replace, go) work consistently no matter the kind of history option is passed when creating the router instance.