Building Modern Web Applications with Vue 3 and Nuxt

Vue and Nuxt logos on a modern gradient background

The landscape of web development continues to evolve, and Vue 3 combined with Nuxt has emerged as a powerful combination for building modern applications. In this comprehensive guide, we'll explore how to leverage these technologies effectively.

Why Vue 3 and Nuxt?#

Vue 3 brought significant improvements to the framework, including the Composition API, better TypeScript support, and improved performance through the new reactivity system. Nuxt builds on top of Vue, providing:

  • Server-Side Rendering (SSR) out of the box
  • File-based routing for intuitive navigation
  • Auto-imports to reduce boilerplate
  • Powerful module ecosystem for common functionality

Setting Up Your Project#

Getting started with Nuxt is straightforward. Here's how to create a new project:

Bash
npx nuxi init my-app
cd my-app
npm install
npm run dev

This creates a minimal Nuxt application with sensible defaults.

The Composition API in Practice#

The Composition API allows us to organize code by logical concern rather than by options. Here's a practical example of a composable:

TypeScript
// composables/useCounter.ts
export function useCounter(initialValue = 0) {
    const count = ref(initialValue);

    function increment() {
        count.value++;
    }

    function decrement() {
        count.value--;
    }

    function reset() {
        count.value = initialValue;
    }

    return {
        count: readonly(count),
        increment,
        decrement,
        reset
    };
}

This composable encapsulates counter logic that can be reused across components.

Type Safety with TypeScript#

Nuxt 3 provides excellent TypeScript support. Here's how to define typed props:

Vue
<script setup lang="ts">
interface Props {
    title: string;
    count?: number;
    items: Array<{ id: number; name: string }>;
}

const props = withDefaults(defineProps<Props>(), {
    count: 0
});
</script>

Data Fetching Patterns#

Nuxt provides powerful composables for data fetching:

TypeScript
// Using useAsyncData for SSR-friendly fetching
const { data, pending, error } = await useAsyncData('posts', () => {
    return $fetch('/api/posts');
});

// Using useFetch as a shorthand
const { data: users } = await useFetch('/api/users');

Performance Optimization#

Here are key strategies for optimizing your Nuxt application:

  1. Lazy Loading Components - Use defineAsyncComponent for heavy components
  2. Image Optimization - Leverage @nuxt/image for automatic optimization
  3. Code Splitting - Nuxt handles this automatically per route
  4. Caching Strategies - Implement proper cache headers

"The best performance optimization is not loading code that isn't needed." โ€” Vue.js Core Team

Conclusion#

Vue 3 and Nuxt provide a robust foundation for building modern web applications. The combination of the Composition API, TypeScript support, and Nuxt's conventions makes development both productive and enjoyable.

In future posts, we'll dive deeper into specific patterns and advanced techniques. Stay tuned!

Share:

Related Articles