zoukankan      html  css  js  c++  java
  • Vuejs 实现简易 todoList 功能 与 组件

    todoList

    结合之前 Vuejs 基础与语法

    • 使用 v-model 双向绑定 input 输入内容与数据 data
    • 使用 @click 和 methods 关联事件
    • 使用 v-for 进行数据循环展示
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="UTF-8">
      <title>TodoList</title>
      <script src="./vue.js"></script>
    </head>
    <body>
      <div id="root">
        <div>
          <input v-model="inputValue"/>
          <button @click="handleSubmit">提交</button>
        </div>
        <ul>
          <li v-for="(item,index) of list" :key="index">
            {{item}}
          </li>
        </ul>
      </div>
    
      <script>
        new Vue({
          el: "#root",
          data: {
            inputValue: '',
            list: []
          },
          methods: {
            handleSubmit: function(){
              this.list.push(this.inputValue)
              this.inputValue = ''
            }
          }
        })
      </script>
    </body>
    </html>
    View Code

    JSbin 预览

    todoList 组件拆分

    Vuejs 组件相关 详细参考组件基础

    全局组件

    注册全局组件,并在 HTML 中通过模板调用组件

        //注册全局组件
        Vue.component('todo-item',{
          template: '<li>item</li>'
        })
        <ul>
          <!-- <li v-for="(item,index) of list" :key="index">
            {{item}}
          </li> -->
          <todo-item></todo-item>      <!-- 通过模板使用组件 -->
        </ul>
    View Code

    JSbin 预览

    局部组件

    在注册了局部组件之后,直接通过模板调用是不可以的,必须要在最外层的 Vue 的实例中添加 components: { }进行组件声明。

        //注册局部组件
        var TodoItem = {
          template: '<li>item</li>'
        }
        new Vue({
          el: "#root",
          components: {   //局部组件需要声明的 components
            'todo-item': TodoItem
          },
          data: {
            inputValue: '',
            list: []
          },
          methods: {
            handleSubmit: function(){
              this.list.push(this.inputValue)
              this.inputValue = ''
            }
          }
        })
    View Code

    JSbin 预览

    即通过局部注册的组件,需要在其他的 Vue 实例中使用该局部组件。必须使用 components 对该局部组件进行注册。
    上面的实例中,要在 Vue 实例中使用 TodoItem 这个局部组件,就通过 todo-item 这个标签来使用。当在实例中 注册好了以后,才可以在模板里面使用这个标签。这样就算正确的使用了局部组件。

    外部传递参数

    给 todo-item 标签添加 :content 属性,值为循环的每一项的内容 "item"
    这样就可以吧 content 传递给 todo-item 这个组件

    <todo-item v-for="(item,index) of list" :key="index" :content="item"></todo-item> 

    但是直接将组件改成是不行的

        Vue.component('todo-item',{
          template: '<li>{{content}}</li>'
        })

    需要让组件接收属性,所以要在todo-item组件里面定义props属性,其值为一个数组 'content' 。
    其含义是,该组件接收从外部传递的进来的名字叫做 content 的属性

        Vue.component('todo-item',{
          props: ['content'],
          template: '<li>{{content}}</li>'
        })

    JSbin 预览

    组件与实例的关系

    Vue 之中,每一个组件其实也是一个 Vue 的实例。因此在 Vue 项目中,是一个个实例构建而成的。
    因此组件之中,也可以绑定 @click 事件,添加 methods 属性。

        Vue.component('todo-item',{
          props: ['content'],
          template: '<li @click="handleClick">{{content}}</li>',
          methods: {
            handleClick: function(){
              alert('clicked')
            }
          }
        })

    JSbin 预览


    同样的实例也可以被称作一个组件,那么我们这个根实例当中的 template 模板是什么呢 ?
    如果一个 Vue 实例没有模板,会到挂载点去找。如下实例,根实例会找到 #root 下面挂载点的所有内容作为模板。

        new Vue({
          el: "#root",
          data: {
            inputValue: '',
            list: []
          },
          methods: {
            handleSubmit: function(){
              this.list.push(this.inputValue)
              this.inputValue = ''
            }
          }
        })

    为 todoList 添加删除功能

    通过 发布 / 订阅,当子组件点击时,通知父组件把数据删除掉。在子组件中,发布自定义一个 'delete' 事件。调用 this.$emit 方法,并传递 index 的值。

    子组件向外部进行发布

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

    父组件在模板里创建子组件的时候,监听子组件向外触发的 delete 事件,如果监听到 delete 事件,执行 handleDelete 函数。

          <todo-item v-for="(item,index) of list"
                     :key="index"
                     :content="item"
                     :index="index"
                     @delete="handleDelete">  <!-- 监听delete事件 -->
          </todo-item>      <!-- 通过模板使用组件 -->

    然后在父组件的 methods 中,写好 handleDelete 方法。

        //最外层实例,父组件
        new Vue({
          el: "#root",
          data: {
            inputValue: '',
            list: []
          },
          methods: {
            handleSubmit: function(){
              this.list.push(this.inputValue)
              this.inputValue = ''
            },
            handleDelete: function(index){
              this.list.splice(index,1)  //使用splice方法删除list
            }
          }
        })

    JSbin 预览

  • 相关阅读:
    隐马尔可夫模型(一)
    酒罢问君三语
    Reveal使用教程
    李洪强和你一起学习前端之(8)CSS复习
    李洪强和你一起学习前端之(7)定位盒子 css可见性 滑动门案例
    李洪强和你一起学习前端之(6)浮动 布局 定位
    李洪强和你一起学习前端之(5)css书写位置 优先级和伪类
    李洪强和你一起学习前端之(4)HTML5介绍
    李洪强和你一起学习前端之(3)Css基础和选择器
    李洪强和你一起学习前端之(2)表格、表单、标签语义化
  • 原文地址:https://www.cnblogs.com/evenyao/p/9608937.html
Copyright © 2011-2022 走看看