LoginSignup
1
0

More than 1 year has passed since last update.

Nuxt.jsでルーティング

Last updated at Posted at 2022-03-21

ルーティング

Nuxt.jsはpagesディレクトリ配下にフォルダ作ってそこにindex.vue設置して上げると自動的にルーティングしてくれます。
便利便利。

ディレクトリ作成→中にindex.vue配置

image.png
こんなかんじで、プロジェクト生成時、デフォルトで生成される[PROJECT]/.nuxt/pages配下にnewPageをディレクトリを作成します。(.nuxtは隠しフォルダなので注意)
index.vueを作成します。

./newPage/index.vue
<template>
    <h1>
      NewPage
    </h1>
</template>

newPageに作ったindexの中身です。とりあえずタイトルだけ表示させやす。

動作確認

$ npm run dev
run dev で確認して見ます。

image.png
できましたね。

補足

ルーティングの設定は、[PROJECT]/.nuxt/router.jsrouterOptionsにオブジェクトとして格納されいます。この中身もいつか詳しくやりたい。

router.jsのrouterOptions抜粋
export const routerOptions = {
  mode: 'history',
  base: '/',
  linkActiveClass: 'nuxt-link-active',
  linkExactActiveClass: 'nuxt-link-exact-active',
  scrollBehavior,

  routes: [{
    path: "/newPage",
    component: _f8688520,
    name: "newPage"
  }, {
    path: "/",
    component: _971975a0,
    name: "index"
  }],

  fallback: false
}
1
0
1

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
1
0