Vaadin.Router supports the redirect property on the route
objects, allowing to unconditionally redirect users from one path to
another. The valid values are a path string or a pattern in the same
format as used for the path property.
The original path is not stored as the window.history entry
and cannot be reached by pressing the "Back" browser button. Unconditional
redirects work for routes both with and without parameters.
The original path is available to route Web Components as the
location.redirectFrom string property, and to custom
route actions –
as context.redirectFrom.
Note: If a route has both the redirect and action
properties, action is executed first and if it does not
return a result Vaadin.Router proceeds to check the redirect
property. Other route properties (if any) would be ignored. In that case
Vaadin.Router would also log a warning to the browser console.
Vaadin.Router allows redirecting to another path dynamically based on
a condition evaluated at the run time. In order to do that,
return commands.redirect('/new/path') from the
onBeforeEnter()
lifecycle callback of the route Web Component.
It is also possible to redirect from a custom route action. The demo below
has an example of that in the /logout route action. See the
Route Actions section for
more details.
If you want to send users to another path in response to a user
action (outside of a lifecycle callback), you can do that by using the
static
Router.go('/to/path') method on the Vaadin.Router class.
NOTE: the same effect can be achieved by dispatching a
vaadin-router-go custom event on the window. The
target path should be provided as event.detail.pathname.