LoginSignup
2
0

More than 3 years have passed since last update.

【Vue.js】ボタンの表示文字列を動的に変更するサンプルコード

Posted at

はじめに

Vue.jsを使って動的にボタンの表示文字列を変更するサンプルコードです。

親要素→子要素のデータの受け渡しを属性propsで行う方法を取っています。

環境

OS: macOS Catalina 10.15.1
Vue: 2.6.10
vue-cli: 4.1.1
vuetify: 2.1.0

出力

image.png

同じコンポーネントを使って、文字列だけを変更しています。

コード

Wrapper.vue
<template>
  <v-row>
    <Button/>
    <v-spacer></v-spacer>
    <Button
      buttonName="サインアップ"
    />
  </v-row>
</template>

<script>
  import Button from './Button.vue'

  export default {
    name: 'Wrapper',
    components: {
      Button,
    }
  }
</script>
Button.vue
<template>
  <v-btn>
    {{ buttonName }}
  </v-btn>
</template>

<script>
  export default {
    name: 'Button',
    props: {
      'buttonName': {
        type: String,
        default: 'ログイン'
      }
    }
  }
</script>

buttonNamedefault: 'ログイン'を指定しているため、Wrapper.vue側ではbuttonName="サインアップ"だけ指定しています。

おわりに

最後まで読んで頂きありがとうございました:bow_tone1:

どなたかの参考になれば幸いです:relaxed:

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