Nuxt
Nuxt is a free and open source JavaScript library based on Vue.js, Nitro, and Vite. Nuxt is inspired by Next.js,[4] which is a similar framework based on React rather than Vue. The main advantage of Nuxt over using Vue alone is its universal rendering system. The framework works as both an in-browser single page application (SPA) as well as a server-rendered static website, by "hydrating" a server-rendered page to a full SPA after it is loaded. This allows websites to have the search engine optimization and performance benefits of a server-rendered site in addition to the interactivity of a client-rendered application.[5][6] Nuxt largely abstracts the server-rendering features from the developer, and it's therefore able to have a similar development experience to a traditional SPA using Vue's single file component (SFC) system.[7] In addition to its flagship universal rendering mechanism, Nuxt also provides many other benefits and quality-of-life features, such as path-based routing, hot module replacement (HMR), TypeScript support out of the box, and middleware and server logic.[8] FeaturesPath-based routingRather than a regular Vue.js application, which ordinarily requires every route to be manually registered, Nuxt uses path-based routing to automatically register every route in an application.[9] Pages are declared in the
Automatic imports
Nuxt automatically imports most Vue composition API functions, and any helper functions from the <script setup>
// ref is automatically imported
const count = ref(0);
// useRoute is also automatically imported
const route = useRoute();
</script>
<template>
<span>{{ count }}</span>
</template>
LayoutsNuxt supports SSR-friendly layouts out of the box, which allows similar pages to use the same basic templates, such as a header and footer. Layouts are declared in the
To enable layouts in a Nuxt project, the entry point of the application, <!-- sample app.vue file content -->
<template>
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
</template>
The default layout is located at <!-- sample layout file content -->
<template>
<CustomNavbar />
<slot />
<CustomFooter />
</template>
A page can use a custom layout by using the <script setup>
definePageMeta({
layout: "custom",
});
</script>
<template>
<!-- this will now fill the slot of the custom layout -->
</template>
MiddlewareNuxt adds middleware support to applications, which enables server logic to run between navigation changes. Both global and page-specific middleware files are supported.[14] Middleware is declared in the export default defineNuxtMiddleware((to, from) => {
// navigation logic
if (to.params.id === "0")
return abortNavigation();
return navigateTo(`/users/${to.params.id}`);
});
Server API
Nuxt can also generate server API routes and handlers, using the // server/api/hello.ts
export default defineEventHandler((event) => {
return {
some: "data here",
};
});
This can now be called from components using the <script setup>
const { data } = await useFetch('/api/hello')
</script>
<template>
<pre>{{ data }}</pre>
</template>
See alsoReferences
External links |