1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Nuxt 3 with Props

Last updated at Posted at 2021-12-23

概要

Nuxt3でPropsを実装する方法

ボタンのクラスは、Tailwindを使用しています。
設定方法は、下記の記事にアップしています!

手順

1. コンポーネントを作成

components/atoms/TheButton.vue
<script setup lang="ts">
interface Props {
  name: string
  to?: string
}

const props = withDefaults(defineProps<Props>(), {
  name: '',
  to: '/'
})
</script>

<template>
  <div>
    <a :href="to" class="text-gray-300 hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">
      {{ name }}
    </a>
  </div>
</template>

2. コンポーネントを呼び出し

pages/index.vue
<script setup lang="ts">
interface Menus {
  name: string
  to: string
}[]

const menus = reactive<Menus>([
  { name: 'ホーム', to: '/home' },
  { name: 'マイページ', to: '/mypage' },
  { name: '設定', to: '/setting' }
])
</script>

<template>
  <div>
    <!-- 単発で呼び出し -->
    <AtomsTheButton name="ホーム" to="/home" />

    <!-- ループで呼び出し -->
    <div v-for="(v, k) in menus" :key="k">
      <AtomsTheButton :name="v.name" :to="v.to" />
    </div>
  </div>
</template>

完了!!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?