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

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

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

12041882-55005438ebec8c8b.png

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

练习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>

12041882-7b461f24cb046ffa.png

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

点击前:

12041882-5f528891b9ce931a.png

点击后:


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

推荐阅读更多精彩内容

  • 把子组件中的数据传递到父组件中例: 注释:点击按钮时,就会弹出父组件传给子组件的数据 练习1: 注释:可以把子组件...
    纪美阅读 310评论 0 0
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,841评论 18 139
  • 1、通过CocoaPods安装项目名称项目信息 AFNetworking网络请求组件 FMDB本地数据库组件 SD...
    阳明AGI阅读 16,003评论 3 119
  • 当赤道留住雪花 眼泪融掉细沙 你肯珍惜我吗 如浮云陪伴天马 公演一个童话 当配乐遗下吉他 画布忘掉了画 请想起我 ...
    短藏刀阅读 207评论 1 1
  • 你见 或者不见我 我就在那里 不悲不喜 你念 或者不念我 情就在那里 不来不去 你爱 或者不爱我 爱就在那里 不增...
    骆小哥儿阅读 380评论 0 0