• 简单的组件间传值


    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Hello World</title>
        <script src="./vue.js"></script>
    </head>
    <body>
    <div id="root">
        <input type="text" v-model="todoValue">
        <button @click="handleBtnClick">提交</button>
        <ul>
            <todo-item v-bind:content="item"
                       v-bind:index="index"
                       v-for="(item,index) in list"
                        @delete="handleItemDelete">
            </todo-item>
        </ul>
    </div>
    <script>
        var TodoItem = {
            props: ['content','index'],
            template: "<li @click='handleItemClick'>{{content}}</li>",
            methods:{
                handleItemClick:function () {
                    this.$emit('delete',this.index);  //点击子组件向父组件触发事件
    
                }
            }
        };
    
    
        var app = new Vue({
            el: '#root',
            components:{
                TodoItem:TodoItem    //注册局部组件
            },
            data: {
                list: [],
                todoValue: ''
            },
            methods: {
                handleBtnClick: function () {
                    this.list.push(this.todoValue);
                    this.todoValue = ''
                },
                handleItemDelete:function (index) {
                    this.list.splice(index,1)  //实现点哪删哪
    
                }
            }
        })
    </script>
    </body>
    </html>
    
    <!--
    逻辑解析
        父组件向子组件传值  用到v-bind   注意子组件需要接受传递过来的值props[]
        子组件向父组件传值  this.$emit 触发事件  相对的父组件在监听该事件
        v-bind:content   简写 :content
        v-on:delete  简写@delete
    -->
  • 相关阅读:
    POJ 1936 All in All
    POJ 2305 Basic remains
    POJ 2081 Recaman's Sequence
    MFC MDI 窗口函数执行顺序
    decompose
    不新建一个文档
    code mistake
    ...
    paper
    stereo
  • 原文地址:https://www.cnblogs.com/xuyxbiubiu/p/9951103.html
Copyright © 2020-2023  润新知