欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

2018-07-22组件之间的互相传值

程序员文章站 2022-07-13 22:45:28
...
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
    <script src='../vue.js'></script>
</head>
<body>
    <div id="app">
        <input type="text" v-model='inputValue'>
        <button v-on:click='handleBtnClick'>提交</button>
        <ul>
            <!-- <li v-for='item in list'>{{item}}</li> -->
            <todo-item 
                v-for='(item ,index) in list' 
                v-bind:content='item'
                v-bind:index='index'
                @delete='handleItemDelete'
                >
            </todo-item>
        </ul>
    </div>

    <script>


        Vue.component('TodoItem',{
            props: ['content','index'],
            template : "<li @click='handleItemClick'>{{content}}</li>",
            methods : {
                handleItemClick : function () {
                    this.$emit('delete',this.index)
                }
            }
        })

        // var TodoItem = {
        //  props: ['content'],
        //  template : "<li @click='handleItemClick'>{{content}}</li>",
        //  methods : {
        //      handleItemClick : function () {
        //          console.log(1)
        //      }
        //  }
        // }

        var vm = new Vue({
            el : '#app' ,
            data : {
                list : [] ,
                inputValue : ''
            },
            methods : {
                handleBtnClick : function () {
                    this.list.push(this.inputValue)
                    this.inputValue=''
                },
                handleItemDelete: function (index) {
                    this.list.splice(index,1)
                }
            }
        })

    </script>
</body>
</html>

父子 : v-bind:

子父 : $emit('事件名','index')

splice(x,y) : 从第几个删除,删除几个