Vue 通過props往子組件通信

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="../vue/dist/vue.min.js"></script>
</head>
<body>
    <div id="app"></div>
</body>
    <script type="text/javascript">
        /*全局組件*/
        Vue.component('Parent',{
            data(){
                return{
                    msg:'父組件信息!'
                }
            },
            template:`
                <div>
                    <p>父組件</p>
                    <Child v-bind:childDate="msg" @changeParentValue="changeParentValue"/>
                </div>
            `,
            methods:{
                changeParentValue(val){
                    console.log(val);
                }
            }
        });
        Vue.component('Child',{
            template:`
                <div>
                <p>子組件</p>
                <input type="text" v-model="childDate" @input="changeValueToParent(childDate)"/>
                </div>
            `,
            props:['childDate'],
            methods:{
                changeValueToParent(val){
                    //console.log(val);
                    this.$emit('changeParentValue',val);
                }
            }
        });
        /*局部組件*/
        var AppTemp = {
            data(){
                return{
                }
            },
            template: `
                <div>
                    <Parent />
                </div>
            `
        };
        new Vue({
            el:'#app',
            data(){
                return{

                }
            },
            components:{
                AppTemp:AppTemp
            },
            template:`<AppTemp />`
        });
    </script>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章