본문 바로가기

Vue/vue 기초 공부하기

vue 기초 - 끝말잇기 만들기

<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>구구단</title>
  <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>

<body>
  <div id="root">
    <div>끝말잇기</div>
    <br>
    <div>{{word}}</div>
    <form v-on:submit="onSubmitForm">
      <input type="text" v-model="value" ref="answer">
      <button type="submit">입력</button>
    </form>
    <div>{{result}}</div>
  </div>
  <script>
    const app = new Vue({
      el: '#root',
      data: {
        word: '유상무',
        result: '',
        value: ''
      },
      methods: {
        onSubmitForm(e) {
          e.preventDefault();
          if (this.word[this.word.length - 1] === this.value[0]) {
            this.result = '정답!';
            this.word = this.value;
          } else {
            this.result = '땡!!!!!!';
          }
          this.value = '';
          this.$refs.answer.focus();
        }
      }
    });
  </script>

</body>

</html>

틀린 답 입력시

 

 

맞는답 입력시