navigateTo
navigateTo
is a router helper function that allows programmatically navigating users through your Nuxt application.
navigateTo
is available on both server side and client side. It can be used within plugins, middleware or can be called directly to perform page navigation.
navigateTo(to: RouteLocationRaw | undefined | null, options?: NavigateToOptions) => Promise<void | NavigationFailure> | RouteLocationRawinterface NavigateToOptions { replace?: boolean redirectCode?: number external?: boolean}
await
or return
on result of navigateTo
when calling it.to
Type: RouteLocationRaw
| undefined
| null
Default: '/'
to
can be a plain string or a route object to redirect to. When passed as undefined
or null
, it will default to '/'
.
options
(optional)Type: NavigateToOptions
An object accepting the following properties:
replace
(optional)boolean
false
navigateTo
pushes the given route into the Vue Router's instance on the client side.replace
to true
, to indicate that given route should be replaced.redirectCode
(optional)number
302
navigateTo
redirects to the given path and sets the redirect code to 302 Found
by default when the redirection takes place on the server side.redirectCode
. Commonly, 301 Moved Permanently
can be used for permanent redirections.external
(optional)boolean
false
true
. Otherwise, navigateTo
will throw an error, as external navigation is not allowed by default.<script setup>// passing 'to' as a stringawait navigateTo('/search')// ... or as a route objectawait navigateTo({ path: '/search' })// ... or as a route object with query parametersawait navigateTo({ path: '/search', query: { page: 1, sort: 'asc' }})</script>
export default defineNuxtRouteMiddleware((to, from) => { // setting the redirect code to '301 Moved Permanently' return navigateTo('/search', { redirectCode: 301 })})
<script setup>// will throw an error;// navigating to an external URL is not allowed by defaultawait navigateTo('https://v3.nuxtjs.org')// will redirect successfully with the 'external' parameter set to 'true'await navigateTo('https://v3.nuxtjs.org', { external: true})</script>