uniapp父页面调用子页面 组件方法记录

文章目录


导文

如何点击父页面,触发子页面函数?

如何点击父页面,修改子页面的值?

如何点击父页面,触发子页面函数

使用的方法是 this.$refs

先写一个子页面的基础内容

主要是要有要被触发的函数,或者数值。

js 复制代码
<template>
    <view class="checkInSharing">
        <uni-popup ref="share" type="share" safeArea backgroundColor="#fff">
            <uni-popup-share></uni-popup-share>
        </uni-popup>
    </view>
</template>

<script>


export default {
    components: {
    },

    data() {
        return {

        }
    },
    props: {
        dateList: {
            type: Object, // 指定dateList应该是一个数组类型
            default: () => { } // 设置默认值,以防父组件没有传递
        }
    },
    // mounted() {

    //     this.checkinDetails()
    // },
    methods: {
        checkinDetails() {
            console.log(`${this.dateList.year}-${this.dateList.month}-${this.dateList.date}`);
            console.log(this.dateList);

        },
        show() {
            console.log(1);
            this.$refs.share.open()
        }
    }
}
</script>

<style lang="scss">
.checkInSharing {}
</style>

父元素

要先把组件引入,给子元素添加ref=

js 复制代码
<checkInSharing :dateList="dateList" ref="checkInSharingPage"></checkInSharing>


import { getCheckinDetails, postCheckinCreate } from '@/api/checkIn.js'
import checkInSharing from '../checkInSharing/index.vue'
export default {
	components: {
		checkInSharing
	},

然后在函数中使用

js 复制代码
handerCheckin() {
			this.$refs.checkInSharingPage.show()
		},

this.$refs.[子页面函数ref名].[子页面函数方法名]

如何点击父页面,修改子页面的值

使用的方法是 this.$refs

先写一个子页面的基础内容

主要是要有要被触发的函数,或者数值。

js 复制代码
<template>
    <view class="checkInSharing">
        <uni-popup ref="share" type="share" safeArea backgroundColor="#fff">
            <uni-popup-share></uni-popup-share>
        </uni-popup>
    </view>
</template>

<script>


export default {
    components: {
    },

    data() {
        return {
           value:0
        }
    },
    props: {
        dateList: {
            type: Object, // 指定dateList应该是一个数组类型
            default: () => { } // 设置默认值,以防父组件没有传递
        }
    },
    // mounted() {

    //     this.checkinDetails()
    // },
    methods: {
        checkinDetails() {
            console.log(`${this.dateList.year}-${this.dateList.month}-${this.dateList.date}`);
            console.log(this.dateList);

        },
        show() {
            console.log(1);
            this.$refs.share.open()
        }
    }
}
</script>

<style lang="scss">
.checkInSharing {}
</style>

父元素

要先把组件引入,给子元素添加ref=

js 复制代码
<checkInSharing :dateList="dateList" ref="checkInSharingPage"></checkInSharing>


import { getCheckinDetails, postCheckinCreate } from '@/api/checkIn.js'
import checkInSharing from '../checkInSharing/index.vue'
export default {
	components: {
		checkInSharing
	},

然后在函数中使用

js 复制代码
handerCheckin() {
			this.$refs.checkInSharingPage.value=1
		},

this.$refs.[子页面函数ref名].[子页面数值名]=[要修改的数值]

您好,我是肥晨。

欢迎关注我获取前端学习资源,日常分享技术变革,生存法则;行业内幕,洞察先机。

相关推荐
疯狂的沙粒2 小时前
在web-view 加载的本地及远程HTML中调用uniapp的API及网页和vue页面是如何通讯的?
前端·uni-app·html
狼性书生14 小时前
uniapp实现的简约美观的星级评分组件
前端·uni-app·vue·组件
Jiaberrr20 小时前
uniapp 安卓 APP 后台持续运行(保活)的尝试办法
android·前端·javascript·uni-app·app·保活
不老刘20 小时前
uniapp+vue3实现CK通信协议(基于jjc-tcpTools)
前端·javascript·uni-app
疯狂的沙粒1 天前
uni-app 如何实现选择和上传非图像、视频文件?
前端·javascript·uni-app
^Rocky1 天前
uniapp 集成腾讯云 IM 富媒体消息(地理位置/文件)
uni-app·腾讯云·媒体
$程1 天前
Uniapp 二维码生成与解析完整教程
前端·uni-app
tryCbest1 天前
UniApp系列
uni-app·web
iOS阿玮1 天前
社交的本质是价值交换,请不要浪费别人的时间。
uni-app·app·apple
monika_yu1 天前
uniapp 开发ios, xcode 提交app store connect 和 testflight内测
uni-app