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 5 years have passed since last update.

vue.jsで簡単なtodoアプリを作る

Posted at

この記事の目的

vue.jsの勉強のために以下の仕様のtodoアプリを作成する。

  • formからsubmitするとtodoに表示される
  • deleteで消せる

v-forでループ処理する

main.js
(function(){
  'use strict';
  var vm = new Vue({
    el: '#app',
    data: {
      todos: [
        'task 1',
        'task 2',
        'task 3'
      ]
    }
  });
})();

main.jsにtodosというプロパティをもって直書きでまずtaskを3つ入れる。これをループで3つとも表示するために

index.html
<!DOCTYPE>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>Myvueapp</title>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <div id="app">
    <ul>
      <li v-for="todo in todos">
        {{ todo }}
      </li>
    </ul>
    <p>
      <input type="text" v-model="name" />
    </p>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  <script src="js/main.js"></script>
</body>
</html>

v-for="todo in todosと記述し、todosの要素をtodoとしてあるだけループするようにする。

submitでtodo追加

イメージ (1)-1.jpg

main.js
(function(){
  'use strict';
  var vm = new Vue({
    el: '#app',
    data: {
      newItem: '',
      todos: [
        'task 1',
        'task 2',
        'task 3'
      ]
    },
    methods: {
      addItem: function(){
        this.todos.push(this.newItem);
        this.newItem = '';
      }
    }
  });
})();

index.html
<!DOCTYPE>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>Myvueapp</title>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <div id="app">
    <ul>
      <li v-for="todo in todos">
        {{ todo }}
      </li>
    </ul>
    <form v-on:submit.prevent=addItem>
      <input type="text" v-model="newItem" />
      <input type="submit" value="Add" />
    </form>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  <script src="js/main.js"></script>
</body>
</html>
  • v-onでsubmit時にaddItemを発火
  • addItem関数でtodosの配列にnewItemを入れる
  • その後値をリセットするために空の値を代入

todoの削除

main.js
(function(){
  'use strict';
  var vm = new Vue({
    el: '#app',
    data: {
      newItem: '',
      todos: [
        'task 1',
        'task 2',
        'task 3'
      ]
    },
    methods: {
      addItem: function(){
        this.todos.push(this.newItem);
        this.newItem = '';
      },
      deleteItem:function(index){
        if(confirm('are you sure?')){
          this.todos.splice(index, 1);
        }
      }
    }
  });
})();
index.html
<!DOCTYPE>
<html lang="ja">
<head>
  <meta charset="utf-8">
  <title>Myvueapp</title>
  <link rel="stylesheet" href="css/style.css">
</head>
<body>
  <div id="app">
    <ul>
      <li v-for="(todo, index) in todos">
        {{ todo }}
        <span @click="deleteItem"></span>
      </li>
    </ul>
    <form v-on:submit.prevent=addItem>
      <input type="text" v-model="newItem" />
      <input type="submit" value="Add" />
    </form>
  </div>
  <script src="https://cdn.jsdelivr.net/npm/vue"></script>
  <script src="js/main.js"></script>
</body>
</html>
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?