Vue 組件&組件之間的通信 之 子組件向父組件傳值


子組件向父組件傳值:子組件通過$.emit()方法以事件形式向父組件發送消息傳值;

使用步驟:

定義組件:現有自定義組件com-a、com-b,com-a是com-b的父組件;

准備獲取數據:父組件com-a要獲取子組件data中的height屬性;

在子組件com-b中,需要用$.emit()方法將數據以事件的形式發送,$.emit('sendData', data, data…),紅色的部分事件名可自定義,數據可傳遞多個;

在父組件中使用子組件的地方 <com-b @自定義事件名='getData'></com-b> 監聽子組件自定義的事件,並且在方法中獲取數據;

在父組件data定義height屬性; 在父組件中實現getData(height)方法,方法參數是子組件傳遞的數據,例如這里直有一個height,然后為this.height賦值; 賦值完畢后就可以使用了;

 

初始時在子組件中定義width和height

 

通過一個button來進行實現子組件向父組件進行傳值

 

 

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>子組件向父組件傳值</title>
    <script type="text/javascript" src="../js/vue.js" ></script>
    </head>
    <body>
        <div>
            <father-component ></father-component>
            </div>
    </body>
    <template id="father-template">
        <div>
        <h2> 父組件</h2>
        myData:<span>{{name}},
            {{id}},
            {{user.username}},{{age}}
        </span><br />
            childData:<span> width:{{width}}</span><br /><span>height:{{height}}</span>
        
        <hr />
      <child-component  :id='id' :age='age' @sent-event='getData'></child-component>    
        </div>
    </template>
    <template id="child-template">
        <div>
            <p> 子組件</p>
            fatherData:<span >{{name}},{{id}},{{user.username}},{{age}}</span><br />
            myData:<span> width:{{width}}</span><br /><span>height:{{height}}</span><br />
            <button @click="setData">向父組件傳送數據</button>
            
        </div>
</template>
    <script>
        
        
        new Vue({
            
            data:{
            
        },
        
            components:{
                    "father-component":{
                        
                        methods:{
            
            getData(width,height){
                this.width=width;
                this.height=height;
            }
        },
                        data(){
                            return{
                                    id:"01",
                                name:'perfect',
                                
                                user:{
                                
                                    username:'博客園---perfect*',
                                    password:'123123'
                                    
                                },
                                age:18,
                                width:0,
                                height:0
                            }
                        },
                    
                        template:'#father-template',
                        
                        components:{
                            "child-component":{
                                
                                methods:{
                                    setData(){
                                        console.log(this);//這里的this指的是child-component實例
                                        this.$emit('sent-event',this.width,this.height);
                                    }
                                },
                                data(){
                                    
                                    return{
                                        width:100,
                                height:50
                                    }
                                },
                                
                                
                                template:'#child-template',
                            
                            
                            
                            
                            props:{
                                
                                name:{
                                    type:String,
                                    //required:true,//必須進行傳值
                                    
                                    default:'perfect*'//定義一個默認值
                                },
                                id:[Number,String],
                                   user:{
                                       type:Object,
                                       default:function(){
                                           return {username:'perfect***',password:'123123'}
                                       }
                                   },
                                   
                                   age:{
                                       type:Number,
                                   validator: function (value) {
                                       return value>=0;
                                   }
                                       
                                       
                                   }
                            }
                            }
                        },
                        
                        
                        
                    }
                }
                
            
            
        }).$mount('div');
    </script>
</html>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM