美文网首页Vue
08-父子组件间的通信:子组件给父组件传值

08-父子组件间的通信:子组件给父组件传值

作者: 乔乔_老师 | 来源:发表于2018-10-25 15:22 被阅读0次
一、子组件给父组件传值(1)
<body>
  <div id='app'>
      <my-father></my-father>
  </div>
   <script src='js/vue.js'></script> 
   <script>
       Vue.component("my-father",{
           template:`
             <div>
               <h1>{{mess}}</h1>
               <my-child @send='rcvMsg'></my-child>
             </div>
           `,
           data:function(){
               return{
                   mess:''
               }
           },
           methods:{
               rcvMsg:function(txt){
                   this.mess=txt
               }
           }
       })
       
       Vue.component('my-child',{
           template:`
                <button @click='sendToFather'>传给父元素</button>
             `,
           data:function(){
               return{
                   msg:'我是子组件中的数据,要给父组件传值'
               }
           },
           methods:{
             sendToFather:function(){
//                 this.$emit('自定义事件名',要传输的数据)
                   this.$emit('send',this.msg)
             }  
           }
       })
       
       
       new Vue({
           el:"#app"
       })
    </script>
</body>
子组件给父组件传值(2)
<body>
   <div id='app'>
       <my-father></my-father>
   </div>
    <script src='js/vue.js'></script>
    <script>
        Vue.component("my-father",{
            template:`
                <div>
                  <h1>我是父组件</h1>
                  <p>组组件传过来的数据:<b>{{mess}}</b></p>
                  <my-child @send='rcvMsg'></my-child>
                </div>
             `,
            data:function(){
                
                return{
                    mess:''
                }
            },
            methods:{
                rcvMsg:function(txt){
                    this.mess=txt
                }
            }
        })
        
        Vue.component('my-child',{
            template:`
             <div>
                <h1>我是子组件</h1>
                <input type='text' v-model='msg'> <button @click='sendMsg'>发送</button>
             </div>
            `,
            data:function(){
                return{
                    msg:''
                }
            },
            methods:{
                sendMsg:function(){
                    this.$emit('send',this.msg)
                }
            }
        })
        
       new Vue({
           el:'#app'
       })
    </script>
</body>
二、父子通信混合使用
<body>
   <div id='app'>
       <chat-room></chat-room>
   </div>
    <script src='js/vue.js'></script>
    <script>
        Vue.component('chat-room',{
            template:`
              <div>
                 <ul>
                    <li v-for="(value,index) in chatcont">{{value}}</li>
                 </ul>
                 <user @send='rcvMsg' userName='jack'></user>
                 <user @send='rcvMsg' userName='rose'></user>
               </div>
            `,
            data:function(){
                return{
                    chatcont:[],
                   
                }
            },
            methods:{
               rcvMsg:function(txt){
                  
                   this.chatcont.push(txt)
               }
            }
        })
        
        Vue.component('user',{
            props:['userName'],
            template:`
             <div>
               <label>{{userName}}</label>
               <input type='text' v-model='msg'>
               <button @click='sendMsg'>发送</button>
            </div>
            `,
            data:function(){
                return{
                    msg:''
                }
            },
            methods:{
                sendMsg:function(){
                    this.$emit('send',this.userName+":"+this.msg)
                }
            }
        })
       new Vue({
           el:'#app'
       })
    </script>
</body>

相关文章

  • react-父子组件间通信

    React-父子组件间通信 父组件传 值 给子组件, 传 方法 给子组件 子组件接收 值 ,触发父组件的 方法

  • 08-父子组件间的通信:子组件给父组件传值

    一、子组件给父组件传值(1) 子组件给父组件传值(2) 二、父子通信混合使用

  • React 父子组件通信

    父子组件通信分为【父组件给子组件传值】、【父组件获取子组件的值】两类。 一.父组件给子组件传值3种方式 1.父组件...

  • Vue.js父子组件和非父子组件间的传值通信

    [toc] 父子组件的传值通信 父组件向子组件传值 父组件: 子组件: 子组件向父组件传值 Note 子组件不能直...

  • vue中的组件通信

    一、组件通信(组件传值) 1.1父子组件通信 1.2子父组件通信 1.3非父子组件通信(兄弟组件通信)

  • Vue组件间通信

    Vue组件间通信 父子组间通信 通过props向子组件传值,通过事件向父级组件发送消息 通过props向子组件传值...

  • vue组件通信

    通常父子组件通信都是用props和$emit进行传递,父组件通过props传值给子组件,子组件通过$emit传值给...

  • vue、react对比

    一、父子通信 1. 父组件通过props向子组件传值 vue:父组件 子组件接收 react:父组件: 子组件: ...

  • 第10节 React propTypes defaultPr

    一、父组件给子组件传值 1.1 defaultProps 父子组件传值中,如果父组件调用子组件的时候不给子组件传值...

  • 第十讲、Vue3.x父组件给子组件传值、Props、Props验

    一、父子组件介绍 二、父组件给子组件传值 1、父组件调用子组件的时候传值 2、子组件接收父组件传值 三、Props...

网友评论

    本文标题:08-父子组件间的通信:子组件给父组件传值

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