0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Nuxt.jsの概要と基本概念を調べてみた

Posted at

Nuxt.jsの概要と基本概念を理解しよう

Nuxt.jsは、Vue.jsベースのフレームワークで、サーバーサイドレンダリング(SSR)や静的サイト生成(SSG)を簡単に実装できる人気のフレームワークです。
本記事では、Nuxt.jsの基本概念と特徴について詳しくまとめました。

Nuxt.jsとは

Nuxt.jsは、Vue.jsアプリケーションを構築するためのフルスタックフレームワークです。
Vue.jsの開発体験を向上させ、本格的なWebアプリケーションを効率的に開発できるように設計されています。

主な特徴

  • サーバーサイドレンダリング(SSR): SEOに優れたアプリケーションを構築
  • 静的サイト生成(SSG): 高速な静的サイトの生成
  • 自動ルーティング: ファイルベースのルーティングシステム
  • モジュールシステム: 豊富なモジュールエコシステム
  • TypeScriptサポート: 型安全な開発環境

Nuxt.jsの基本概念

1. ファイルベースルーティング

Nuxt.jsでは、pagesディレクトリ内のファイル構造がそのままルート構造になります。

pages/
├── index.vue          # / ルート
├── about.vue          # /about ルート
└── users/
    ├── index.vue      # /users ルート
    └── [id].vue       # /users/:id ルート

2. レイアウトシステム

layoutsディレクトリでレイアウトを定義し、ページで使用できます。

<!-- layouts/default.vue -->
<template>
  <div>
    <header>ヘッダー</header>
    <main>
      <slot />
    </main>
    <footer>フッター</footer>
  </div>
</template>

3. コンポーネント自動インポート

componentsディレクトリ内のコンポーネントは自動的にインポートされます。

components/
├── Header.vue
├── Footer.vue
└── ui/
    └── Button.vue

主要機能

1. サーバーサイドレンダリング(SSR)

Nuxt.jsの最大の特徴の一つがSSRです。これにより、SEOに優れたアプリケーションを構築できます。

<!-- pages/index.vue -->
<template>
  <div>
    <h1>{{ title }}</h1>
    <p>{{ description }}</p>
  </div>
</template>

<script setup>
// サーバーサイドで実行される
const { data } = await $fetch('/api/hello')
const title = data.title
const description = data.description
</script>

2. データフェッチング

Nuxt.jsでは、useFetch$fetchを使用してデータを取得できます。

<script setup>
// クライアントサイドとサーバーサイドの両方で動作
const { data: posts } = await useFetch('/api/posts')

// リアクティブなデータフェッチング
const { data: user } = await useFetch('/api/user', {
  key: 'user',
  transform: (data) => data.user
})
</script>

3. 状態管理

Nuxt.jsでは、useStateを使用してリアクティブな状態管理ができます。

<script setup>
// グローバル状態
const counter = useState('counter', () => 0)

// 状態の更新
const increment = () => {
  counter.value++
}
</script>

設定ファイル

nuxt.config.ts

Nuxt.jsの設定はnuxt.config.tsで行います。

export default defineNuxtConfig({
  // 開発設定
  devtools: { enabled: true },
  
  // CSS設定
  css: ['~/assets/css/main.css'],
  
  // モジュール
  modules: [
    '@nuxtjs/tailwindcss',
    '@pinia/nuxt'
  ],
  
  // ランタイム設定
  runtimeConfig: {
    // サーバーサイドでのみ利用可能
    apiSecret: '123',
    // クライアントサイドでも利用可能
    public: {
      apiBase: '/api'
    }
  }
})

ディレクトリ構造

Nuxt.jsプロジェクトの典型的なディレクトリ構造は以下の通りです。

project/
├── assets/          # 未処理のアセット
├── components/      # Vueコンポーネント
├── composables/     # コンポーザブル関数
├── layouts/         # レイアウト
├── middleware/      # ミドルウェア
├── pages/           # ページ
├── plugins/         # プラグイン
├── public/          # 静的ファイル
├── server/          # サーバーサイドコード
├── stores/          # Piniaストア
├── nuxt.config.ts   # 設定ファイル
└── package.json

まとめ

Nuxt.jsは、Vue.jsアプリケーションの開発を大幅に簡素化する強力なフレームワークです。SSR、SSG、自動ルーティングなどの機能により、SEOに優れた高性能なWebアプリケーションを効率的に構築できます。

特に以下のような場面でNuxt.jsの真価を発揮します:

  • SEOが重要なWebサイト
  • 高速な静的サイト
  • 本格的なWebアプリケーション
  • モダンな開発体験を求めるプロジェクト

Nuxt.jsを学ぶことで、Vue.jsの可能性を最大限に活用したアプリケーション開発が可能になります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?