preloadRouteComponents
preloadRouteComponents 允许你在 Nuxt 应用中手动预加载单个页面。
预加载路由会加载用户将来可能导航到的特定路由的组件。这可以确保组件更早可用,从而减少阻塞导航的可能性,进而提升性能。
如果你正在使用
NuxtLink 组件,Nuxt 已经会自动预加载必要的路由。示例
在使用 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 不会产生任何效果。