vue2.5 todolist

今天在慕课网上看最新视频看见的,一晚上学完,感觉很好,学到很多虽基础,但很重要的东西,建议有基础的人看一下。
https://www.imooc.com/learn/980

单文件开发:

<!DOCTYPE html>
<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>Start Vue2.5</title>
  <script src="./vue.js"></script>
</head>
<body>
  <div id="root">
    <div>
      <input v-model="inputValue"/>
      <button @click="handleSubmit">提交</button>
    </div>
    <ul>
      <todo-item
        v-for="(item, index) of list"
        :key="index"
        :content="item"
        :index="index"
        @delete="handleDelete"
      ></todo-item>
    </ul>
  </div>

  <script>
    // 全局组件
    Vue.component('todo-item', {
      props: ['content', 'index'],
      template: '<li @click="handleClick">{{content}} {{index}}</li>',
      methods: {
        handleClick() {
          this.$emit('delete', this.index)
        }
      }
    })

    // 局部组件,需要在父组件中注册
    // var TodoItem = {
    //   props: ['content', 'index'],
    //   template: '<li @click="handleClick">{{content}} {{index}}</li>'
    // }

    new Vue({
      el: "#root",
      data: {
        inputValue: '',
        list: []
      },
      methods: {
        handleSubmit() {
          this.list.push(this.inputValue)
          this.inputValue = ''
        },
        handleDelete(i) {
          console.log(i)
          this.list.splice(i, 1)
        }
      }
    })
  </script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/s1124yy/article/details/79999188