preloadRouteComponents
preloadRouteComponents 允许你在 Nuxt 应用中手动预加载单个页面。
预加载路由会加载用户将来可能导航到的指定路由的组件。这确保了组件可以更早地使用,并且不太可能阻塞导航,从而提高性能。
¥Preloading routes loads the components of a given route that the user might navigate to in future. This ensures that the components are available earlier and less likely to block the navigation, improving performance.
如果你使用
NuxtLink
组件,Nuxt 已经自动预加载了必要的路由。¥Nuxt already automatically preloads the necessary routes if you're using the NuxtLink
component.示例
¥Example
使用 navigateTo
时预加载路由。
¥Preload a route when using navigateTo
.
// we don't await this async function, to avoid blocking rendering
// this component's setup function
preloadRouteComponents('/dashboard')
const submit = async () => {
const results = await $fetch('/api/authentication')
if (results.token) {
await navigateTo('/dashboard')
}
}
在服务器上,
preloadRouteComponents
将不起作用。