其他分享
首页 > 其他分享> > vue 父子组件传值

vue 父子组件传值

作者:互联网

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
    <div id="app">
        <input type="text" v-model="inputValue">
        <button @click="press">press</button>
        <ul>
           <todo-item :content="i"
                      v-for="(i,index) in list"
                      :index="index"
                      @delete="deleteChild(index)"></todo-item>
        </ul>
    </div>

    <script>

        let todoItem = {
            props:['content'],
            template:'<li @click="press">{{content}}</li>',
            methods: {
                press(target) {
                    this.$emit('delete')
                }
            }
        }
        const app = new Vue({
            el:'#app',
            data:{
                list:['第一','第二'],
                inputValue:''
            },
            components:{
              TodoItem:todoItem
            },
            methods:{
                press: function () {
                    // alert("press")
                    this.list.push(app.$data.inputValue)
                    this.inputValue = ''
                },
                deleteChild:function (childNode) {
                    console.log(childNode)
                    this.list.splice(childNode,1)
                }
            }
        })

       
    </script>

</body>
</html>

 

 

子组件通过 

 this.$emit('delete')

的方法通知父组件删除子组件

 

标签:vue,app,list,press,inputValue,组件,传值,childNode
来源: https://www.cnblogs.com/lyr-2000/p/13892041.html