使用Vue做一个简单的todo应用的三种方式

1. 引用vue.js

<!DOCTYPE html>
<html>
<head>
<script src="http://vuejs.org/js/vue.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body>
  <div id="root">
    <input type="text" v-model="inputValue">
    <button @click="handlerAdd">提交</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() {
          this.lists.push(this.inputValue);
          this.inputValue = '';
        },
        handlerDel: function(index) {
          this.lists.splice(index, 1);
        }
      }
    });
  </script>
</body>
</html>

2. 全局组件注册

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

    });

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

3. vue-cli脚手架

// Todo.Vue

<template>
  <div>
    <input type="text" v-model="inputValue">
    <button @click="handlerAdd">提交</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 () {
      this.lists.push(this.inputValue)
      this.inputValue = ''
    },
    handlerDel (index) {
      this.lists.splice(index, 1)
    }
  },
  components: {
    'todo-item': TodoItem
  }
}
</script>

<style>

</style>
// TodoItem.vue

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

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

<style scoped>
  ul,li {
    list-style: none;
  }
  .item {
    color: blueviolet;
  }
</style>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 我第一次听到花儿绽放的声音是在我七岁的时候,放学回家看到一簇迎春花在乍暖还寒的时候,显得格外引人注意,于是我...
    别再说我要努力阅读 466评论 1 1
  • 上一章:原来爱上你(三十):以身试情 原来爱上你目录 电话声响起。是刘建。 杨律师将手机调为静音,任由刘建在电话那...
    洋阿洋阅读 666评论 0 2
  • source命令用法:source FileName 作用:在当前bash环境下读取并执行FileName中的命令...
    Ten_Minutes阅读 5,711评论 0 3
  • 一本经书,一杯淡茶,一首轻歌,一间茅屋,一个人,静享思绪蔓延,品味人生起伏。世界需要沉淀,因为是你让我思考,感谢生...
    沐恩静侯阅读 234评论 0 0