初识Vue,简单的todolist

本文介绍了一个使用Vue.js实现的TodoList应用案例,通过具体代码展示了如何在Vue项目中进行双向数据绑定、子组件与父组件之间的通信及事件处理等关键技术点。

vue开发源码:https://vuejs.org/js/vue.js

todolist代码:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>vue入门</title>
  <script src="vue.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
  <div id="app">
    <input v-model="inputValue" type="text" name="">
    <button @click="submit">提交</button>
   
    <ul>
      <todo-item 
        v-for="(item, index) in list" 
        :key="index" 
        :content="item" 
        :index="index" 
        @delete="handleDelete"
        @update="handleUpdate"
        >
      </todo-item>
    </ul>
    <input v-model="updateValue" type="text" name="">
    <button @click="update">确定</button>
  </div>
</body>
<script type="text/javascript">

  Vue.component("todo-item", {
    props:["content", "index"],
    template:'<li>{{content}} <button @click="handleClick">remove</button><button @click="handleUp">update</button></li>',
    methods:{
      handleClick:function() {
        this.$emit('delete', this.index)
      },
      handleUp:function() {
        this.$emit('update', this.index)
      }
    }
  })

  new Vue({
    el:"#app",
    data: {
      inputValue : '',
      updateValue : '',
      in:'',
      list:[]
    },
    methods: {
      submit:function() {
        if(this.inputValue.trim() !== "") {
          this.list.push(this.inputValue);
        }
          this.inputValue = ''
      },
      handleDelete: function(index) {
        this.list.splice(index, 1);
      },
      handleUpdate: function(index) {
        this.updateValue = this.list[index]
        this.in = index;
      },
      update: function() {
        console.log(this.in)
        this.list.splice(this.in, 1, this.updateValue);
        this.updateValue = ''
      }
    }
  })
</script>
</html>

  重点:子组件与父组件的值传递

转载于:https://www.cnblogs.com/detanx/p/vueTodolist.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值