SoFunction
Updated on 2025-04-06

Sample code of three ways to make a simple todo application using Vue

1. Quote

<!DOCTYPE html>
<html>
<head>
<script src="/js/"></script>
 <meta charset="utf-8">
 <title>JS Bin</title>
</head>
<body>
 <div >
  <input type="text" v-model="inputValue">
  <button @click="handlerAdd">submit</button>
  <ul>
   <li 
     v-for="(item,index) of lists" 
     :key="index" 
     @click="handlerDel(index)"
    >
    {{item}}
   </li>
  </ul>
 </div>
 
 <script>
  new Vue({
   el: '#root',
   data: {
    inputValue: '',
    lists: []
   },
   methods: {
    handlerAdd: function() {
     ();
      = '';
    },
    handlerDel: function(index) {
     (index, 1);
    }
   }
  });
 </script>
</body>
</html>

2. Global component registration

<!DOCTYPE html>
<html>
<head>
<script src="/js/"></script>
 <meta charset="utf-8">
 <title>JS Bin</title>
</head>
<body>
 <div >
  <input type="text" v-model="inputValue">
  <button @click="handlerAdd">submit</button>
  <ul>
   <todo-item
    v-for="(item,index) of lists"
    :content = "item"
    :index = "index"
    :key = "index"
    @delete="handlerDel"
   >
   </todo-item>
  </ul>
 </div>
 
 <script>
  ('todoItem', {
   props: {
    content: String,
    index: Number
   },
   template: '<li @click="handlerClick">{{content}}</li>',
   methods: {
    handlerClick: function(){
     this.$emit('delete', );
    }
   }

  });

  new Vue({
   el: '#root',
   data: {
    inputValue: '' ,
    lists: []
   },
   methods: {
    handlerAdd: function() {
     ();
      = '';
    },
    handlerDel: function(index) {
     (index,1);
    }
   }
  });
 </script>
</body>
</html>

3. vue-cli scaffolding

// 

<template>
 <div>
  <input type="text" v-model="inputValue">
  <button @click="handlerAdd">submit</button>
  <ul>
   <todo-item
    v-for="(item,index) of lists"
    :key="index"
    :content="item"
    :index="index"
    @delete="handlerDel"
   ></todo-item>
  </ul>
 </div>
</template>

<script>
import TodoItem from './components/todoItem'

export default {
 data () {
  return {
   inputValue: '',
   lists: []
  }
 },
 methods: {
  handlerAdd () {
   ()
    = ''
  },
  handlerDel (index) {
   (index, 1)
  }
 },
 components: {
  'todo-item': TodoItem
 }
}
</script>

<style>

</style>
// 

<template>
 <li @click="handlerClick" class="item">{{content}}</li>
</template>

<script>
export default {
 props: ['content', 'index'],
 methods: {
  handlerClick () {
   this.$emit('delete', )
  }
 }
}
</script>

<style scoped>
 ul,li {
  list-style: none;
 }
 .item {
  color: blueviolet;
 }
</style>

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.