• 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>
  • 相关阅读:
    常用正则表达式
    在过滤器中设置一个应用范围内的路径
    在过滤器中设置一个应用范围内的路径
    shell編程之遠程開啓多個機器的ElasticSearch
    shell之CMD的使用,是用` `還是" "?
    shell編程之自動化集群搭建並啓動
    shell編程之權限
    登录用户出现‘’-bash-4.2$‘’的问题解决
    locate: can not open `/var/lib/mlocate/mlocate.db': No such file or directory
    Spring之mvc應用(包含aop)
  • 原文地址:https://www.cnblogs.com/500m/p/11776551.html
Copyright © 2020-2023  润新知