美文网首页
组件:子传父 对话框

组件:子传父 对话框

作者: 雨笑_e29c | 来源:发表于2018-09-24 08:34 被阅读0次

    把子组件中的数据传递到父组件中

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>子给父传值</title>

    </head>

    <body>

    <div class="itany">

    <father></father>

    </div>

    <script src="./vue.js"></script>

    <script>

    Vue.component('father', {

    template: `

    <div>

    <h1>{{num2}}</h1> <child @meth='the'></child>

    </div> `,

    data: function() {

    return {

    num2: ''

    }

    },

    methods: {

    the: function(txt) {

    this.num2 = txt

    }

    }

    })

    Vue.component('child', {

    template: ` <button @click="show">传给父元素</button> `,

    data: function() {

    return {

    num: '我是子组件,我要传值给父组件'

    }

    },

    methods: {

    show: function() {

    this.$emit('meth', this.num)

    }

    }

    })

    new Vue({

    el: '.itany'

    })

    </script>

    </body>

    </html>


    注释:可以把子组件中input的value传递到父组件的p标签中去

    练习2:制作一个聊天对话框

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>对话框</title>

    </head>

    <body>

    <div class="itany">

    <father></father>

    </div>

    <script src="vue.js"></script>

    <script>

    Vue.component('father', {

    template: `

    <div>

    <ul> <li v-for="(value,index) in one">{{value}}</li>

    </ul> <child @show="the" userName='jack'></child>

    <child @show="the" userName='roce'></child>

    </div> `,

    data: function() {

    return {

    one: []

    }

    },

    methods: {

    the: function(txt) {

    this.one.push(txt)

    }

    }

    })

    Vue.component('child', {

    props: ['userName'],

    template: `

    <div> <label>{{userName}}</label> <input type="text" v-model="arr"> <button @click="add">发送</button>

    </div> `,

    data: function() {

    return {

    arr: ''

    }

    },

    methods: {

    add: function() {

    this.$emit('show', this.userName + ':' + this.arr);

    this.arr = ""

    }

    }

    })

    new Vue({

    el: ".itany"

    })

    </script>

    </body>

    </html>


    点击按钮时,就会弹出父组件传给子组件的数据

    <!DOCTYPE html>

    <html lang="en">

    <head>

    <meta charset="UTF-8">

    <title>子给父传值练习</title>

    </head>

    <body>

    <div class="itany">

    <father></father>

    </div>

    <script src="vue.js"></script>

    <script>

    Vue.component('father', {

    template: `

    <div> <h1>我是父元素</h1> <p>请传值<b>{{mess}}</b></p> <child @show="the"></child>

    </div> `,

    data: function() {

    return {

    mess: ""

    }

    },

    methods: {

    the: function(txt) {

    this.mess = txt

    }

    }

    })

      Vue.component('child', {

    template: `

    <div>

    <h1>我是子元素</h1> <input type="text" v-model="arr"> <button @click="add">传值</button>

    </div> `,

    data: function() {

    return {

    arr: ""

    }

    },

    methods: {

    add: function() {

    this.$emit('show', this.arr)

    }

    }

    }) 

      new Vue({

    el: '.itany'

    })

    </script>

    </body>

    </html>

    相关文章

      网友评论

          本文标题:组件:子传父 对话框

          本文链接:https://www.haomeiwen.com/subject/czjcoftx.html