首页 > 移动平台 > 详细

01_uni-app中vue组件父子值传递

时间:2020-03-22 16:40:42      阅读:150      评论:0      收藏:0      [点我收藏+]

一、父组件向子组件传递数据(props)
child:
<template>
    <view class="container" style="background: #0062CC;">        
        <view class="child"> hi {{showModal}}</view>
    </view>
</template>
<script>
    export default {
        props: {
            showModal: {
                type: String,
                default: ‘hello‘
            }
        },
        data() {
            return {
                   
            };
        },methods:{
            
        }
    }
</script>
<style>
</style>

parent:
<template>
    <view>
        <child :showModal="showModal"></child>
    </view>
</template>
<script>
    import child from "../../components/child.vue"
    export default {
        components: {
            child
        },
        data() {
            return {
                showModal: " parent say"
            };
        },
        methods: {

        }
    }
</script>
<style>
</style>

二、子组件向父组件传递数据(emit)
parent:
<template>
    <view>
        <child :showModal="showModal" @changes="childClick"></child>
        <view>{{parentValue}}</view>
    </view>
</template>
<script>
import child from "../../components/child.vue"    
    export default {
        components:{
            child
        },
        data() {
            return {
                showModal:" parent say",
                parentValue:‘‘
            };
        },methods:{
            childClick(e){
                console.info(e);
                this.parentValue=e;
            }
                
        }
    }
</script>
<style>
</style>

child:
<template>
    <view class="container">
        <button @tap="childClick" >点击我 </button>
        <view class="child"> hi  {{showModal}}</view>
    </view>
</template>
<script>
    export default {
        props: {
            showModal: {
                type: String,
                default: ‘hello‘
            }
        },
        data() {
            return {
                   childdata:‘child value‘
            };
        },methods:{
            childClick(){
                console.info(this.childdata);
                this.$emit("changes",this.childdata);
            }
        }
    }
</script>
<style>
</style>

三、子组件与父组件数据同步(.sync)
child:
<template>
    <view class="container" style="background: #0062CC;">
        <button @tap="childClick" >点击我 </button>
        <view class="child"> hi  {{showModal}}</view>
         <view>psync同步(child):{{syncDate}}</view>
    </view>
</template>
<script>
    export default {
        props: {
            showModal: {
                type: String,
                default: ‘hello‘
            },
            syncDate: {
                type: String,
                default: ‘hello‘
            }
        },
        data() {
            return {
                   childdata:‘child value‘
            };
        },methods:{
            childClick(){
                console.info(this.childdata);
                this.$emit("changes",this.childdata);
            }
                
        }
    }
</script>
<style>
</style>

parent:
<template>
    <view>
        <child :syncDate.sync=‘syncDate‘ :showModal="showModal" @changes="childClick"></child>

        <view class="parent" style="background: #09BB07;">
            <view>emit传值:{{parentValue}}</view>
              <input :value="syncDate" v-model="syncDate" style="background: #808080;" />
            <view>psync同步(parent):{{syncDate}}</view>
        </view>
    </view>
</template>
<script>
    import child from "../../components/child.vue"
    export default {
        components: {
            child
        },
        data() {
            return {
                showModal: " parent say",
                parentValue: ‘‘,
                syncDate: ‘ p syncDate‘
            };
        },
        methods: {
            childClick(e) {
                console.info(e);
                this.parentValue = e;
            }

        }
    }
</script>
<style>
</style>

01_uni-app中vue组件父子值传递

原文:https://www.cnblogs.com/luwei0915/p/12546375.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!