美文网首页
2018-09-21非父子,父子组件传值和复习

2018-09-21非父子,父子组件传值和复习

作者: 萧声断未央 | 来源:发表于2018-09-22 09:03 被阅读0次
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
    <!--   
        1.父给子传值   属性  props:['属性']  
        2.子给父传    用事件传 $emit
        3.非父子      用第三方量

     -->
       <div id='app'>
           <child></child>
           <son></son>
       </div>
        <script src='js/vue.js'></script>
        <script>
           var bus=new Vue() 
    
           Vue.component('child',{
               template:`
                  <div>
                     <h1>这是组件A</h1>
                     <button @click='sendMsg'>点击按钮传值</button>
                  </div>
               `,
               data:function(){
                   return{
                       msg:'非父子组件传值'
                   }
               },
               methods:{
                   sendMsg:function(){
                      bus.$emit('send',this.msg)
                   }
               }
           }) 
    
          Vue.component('son',{
              template:`
                 <div>
                     <h1>这是组件B</h1>
                     <a href="#">{{mess}}</a>
                  </div>
              `,
              data:function(){
                  return{
                      mess:''
                  }
              },
              mounted:function(){
                 bus.$on('send',msg=>{
                     console.log(this);
                     this.mess=msg;
                 }) 
              }
      
          })  
   
    
          new Vue({
              el:'#app'
          })
        </script>
    </body>
    </html>




    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <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>
    </html>



    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Document</title>
    </head>
    <body>
        <div id='app'>
            <my-component></my-component>
        </div>
        <script src='js/vue.js'></script>
        <script>
           Vue.component('my-component',{
               template:`<div>
                     <h1>我是父组件</h1>
                     <my-child v-bind:message='msg' @send='rcvMsg'></my-child>
                     <b>{{number}}</b>
                  </div>
               `,
               data:function(){
                   return{
                       msg:'hello vue',
                       number:''
                   }
               },
               methods:{
                   rcvMsg:function(txt){
                       alert(111)
                       this.number=txt
                   }
               }
       
           })
   
           Vue.component("my-child",{
               props:['message'],
               template:`
                 <div>
                     <h2>我是子元素</h2>
                     <a href='#'>{{message}}</a>
                     <button @click='sendFather'>发送给父组件</button>
                 </div>
                `,
               data:function(){
                   return{
                       num:5
                   }
               },
               methods:{
                   sendFather:function(){
                       alert(111)
                       this.$emit('send',this.num)
                   }
               }
           })
   
   
           new Vue({
               el:'#app'
           })
        </script>
    </body>
    </html>

相关文章

网友评论

      本文标题:2018-09-21非父子,父子组件传值和复习

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