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

2018-07-22  本文已影响0人  无欲而为
<!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) : 从第几个删除,删除几个

上一篇下一篇

猜你喜欢

热点阅读