zoukankan      html  css  js  c++  java
  • vue 【 子子组件 => 子组件 => 父组件 】 的事件和参数的传递

    1,子子组件  TodoItem.vue    

    <template>
      <div class="todo-item" :class="{'is-complete':todo.completed}">
          <p>
              <input type="checkbox" @change="markComplete">
              {{todo.title}}
              <button class="del" @click="$emit('deleteItem',todo.id)">x</button>
          </p>
      </div>
    </template>

    <script>
    export default {
        name:'todo',
        props:["todo"],
        methods:{
            markComplete(){
                this.todo.completed = !this.todo.completed
            }
        }
    }

    </script>

    <style scoped>
    .todo-item{
        background: #f4f4f4;
        padding: 10px;
        border-bottom: 1px dotted #ccc ;
    }

    .is-complete{
        text-decoration: line-through
    }

    .del{
        background: #ff0000;
        color: #fff;
        border: none;
        padding: 5px 9px;
        border-radius: 50%;
        cursor: pointer;
        float: right;
        outline: none
    }
    </style>
     
    2,子组件 Todos.vue
    <template>
      <div>
        <div v-for="todo in todos" :key="todo.id">
          <TodoItem :todo="todo" @deleteItem="deleteItem"/>
        </div>
      </div>
    </template>

    <script>
    import TodoItem from './TodoItem'
    export default {
        name:'todos',
        props:["todos"],
        components:{
          TodoItem:TodoItem
        },
        methods:{
          deleteItem(id){
            this.$emit('handleDelete',id)
          }
        }
    }

    </script>
     
    3,父组件 app.vue 
    <template>
      <div id="app">
         <Todos :todos="todos" @handleDelete="handleDelete"/>
      </div>
    </template>

    <script>
    import Todos from './components/Todos'
    export default {
      name:'app',
      data(){
        return{
          todos:[
            {id:1,title:"待办事项1",completed:false},
            {id:2,title:"待办事项2",completed:false},
            {id:3,title:"待办事项3",completed:false},
          ]
        }
      },
      components:{
        Todos:Todos
      },
      methods:{
        handleDelete(id){
          console.log('id :'+id);
          this.todos = this.todos.filter(todo => todo.id != id);
        }
      }
    }
    </script>

    <style>
    *{margin: 0;padding: 0;box-sizing: border-box}
    </style>
  • 相关阅读:
    loadView和viewDidLoad的官方API的一些解释
    面向对象IOS编程中的聚合与耦合
    史上最全的iOS面试题及答案
    oc的基本数据的转换
    NSDateFormatter时间函数
    图片点击放大,再次点击返回原视图.完美封装,一个类一句代码即可调用.IOS完美实现
    同一ViewController内如果有两处AlertView要用代理Delegate怎么写法
    简单实现---下拉刷新 --使用UITableViewController中的refreshControl属性
    简单实现---上拉加载刷新---
    洛谷2863 [Usaco06JAN]牛的舞会
  • 原文地址:https://www.cnblogs.com/500m/p/11776551.html
Copyright © 2011-2022 走看看