组件:子传父 对话框小练习

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

 <!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>

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

练习1:

<!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>

注释:可以把子组件中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>

点击前:



点击后:


©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,131评论 19 139
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AI阅读 16,026评论 3 119
  • 山上有一栋房子,房子里面住着小男孩和他的爸爸妈妈、爷爷奶奶。他们的屋子下面有个老鼠洞。 一个春天的早上,天气晴朗,...
    铭宇记录阅读 344评论 0 0
  • 今天上午,王奇老师分享了“归零自在”,“生命的状态”。 只有归零,才无分别;只有归零,才没有敌我对立;只有归零,才...
    金海锦阅读 628评论 0 2
  • 梦里遥忆慈母迟, 醒来已是两枕湿。 空负男儿五尺躯, 奈何桥头力有时。 桃花三月春正好, 炊烟如昔客如织。 201...
    沙了个鸥阅读 218评论 0 0