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?

More than 1 year has passed since last update.

仕事で使った、Vueのcase文の復習をしてみました。

<template>
  <div>
    <button v-for="btn in btns"
      :key="btn.id"
      @click="btnClicked(btn)">
      {{btn.text}}
    </button>
    <p>{{message}}</p>
  </div>
</template>
<script>
export default{
  data(){
    return{
      btns:[
        {
          cmd:'methodA',
          text:'btnA'
        },
        {
          cmd:'methodB',
          text:'btnB'
        }
      ],
        message:"not clicked"
    }
  },
  methods:{
    btnClicked(command){
      switch(command.cmd){
        case 'methodA':
          this.methodA()
          break
        case 'methodB':
          this.methodB()
          break
        default:
          this.methodOthers()
      }
    },
    methodA(){
      this.message = "This is A"
    },
    methodB(){
      this.message = "This is B"
      alert("Bだよ!")
    },
    methodOthers(){
      this.message = "othersClicked"
    }
  }
}
</script>

実行結果はこちらです。
image.png
ボタンをクリック後はこうなります。

image.png
image.png

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?