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

在这里插入图片描述


导文

如何点击父页面,触发子页面函数?
如何点击父页面,修改子页面的值?

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

使用的方法是 this.$refs

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

在这里插入图片描述
主要是要有要被触发的函数,或者数值。

<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=

<checkInSharing :dateList="dateList" ref="checkInSharingPage"></checkInSharing>


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

然后在函数中使用

handerCheckin() {
			this.$refs.checkInSharingPage.show()
		},

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

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

使用的方法是 this.$refs

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

在这里插入图片描述
主要是要有要被触发的函数,或者数值。

<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=

<checkInSharing :dateList="dateList" ref="checkInSharingPage"></checkInSharing>


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

然后在函数中使用

handerCheckin() {
			this.$refs.checkInSharingPage.value=1
		},

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

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

### 关于 UniApp “我的页面组件的实现 在 UniApp 开发中,“我的页面”通常是一个常见的功能模块,用于展示用户的个人信息、设置选项以及其他相关内容。以下是构建“我的页面”的一些核心概念和代码示例。 #### 1. 页面结构设计 “我的页面”可以通过 `view` 容器和其他基础组件来搭建布局。以下是一个简单的模板结构: ```html <template> <view class="my-page"> <!-- 用户头像 --> <view class="avatar-section"> <image class="avatar" src="/static/avatar.png"></image> <text class="username">用户名</text> </view> <!-- 功能列表 --> <view class="list-section"> <navigator url="/pages/settings/settings" hover-class="none"> <view class="item"> <text>个人资料</text> <icon type="arrowright" size="20"/> </view> </navigator> <navigator url="/pages/orders/orders" hover-class="none"> <view class="item"> <text>订单管理</text> <icon type="arrowright" size="20"/> </view> </navigator> </view> <!-- 登出按钮 --> <button @click="logout">退出登录</button> </view> </template> ``` 此部分展示了如何使用视图容器组件[^1]以及导航链接 (`navigator`) 来创建一个基本的功能列表。 --- #### 2. 样式定义 为了使界面更加美观,可以为上述模板添加样式: ```css .my-page { padding: 20px; } .avatar-section { display: flex; align-items: center; justify-content: center; margin-bottom: 20px; } .avatar { width: 80px; height: 80px; border-radius: 50%; } .username { font-size: 18px; margin-top: 10px; } .list-section .item { display: flex; justify-content: space-between; align-items: center; padding: 10px; border-bottom: 1px solid #ccc; } ``` 以上 CSS 使用了 Flexbox 布局技术,使得页面更具响应性和可读性。 --- #### 3. 脚本逻辑 脚本部分主要用于处理交互事件,例如点击跳转或登出操作: ```javascript <script> export default { data() { return {}; }, methods: { logout() { uni.showModal({ title: '提示', content: '确认要退出登录吗?', success(res) { if (res.confirm) { console.log('用户点击确定'); // 执行登出逻辑 } else if (res.cancel) { console.log('用户点击取消'); } }, }); }, }, }; </script> ``` 此处通过调用 `uni.showModal()` 方法实现了弹窗提示效果。 --- #### 4. 生命周期注意事项 如果需要初始化数据或者监听某些状态变化,可以在组件的生命周期钩中完成。例如,在 `onLoad` 阶段获取用户信息: ```javascript onLoad(options) { this.getUserInfo(); }, methods: { getUserInfo() { uni.request({ url: '/api/user/info', // 替换为实际接口地址 method: 'GET', success(res) { const userInfo = res.data || {}; console.log(userInfo); }, }); }, }, ``` 需要注意的是,当动态控制组件显示隐藏时(如使用 `v-if`),可能会遇到生命周期未触发的情况。此时可通过调整条件渲染方式解决[^3]。 --- #### 5. 性能优化建议 对于复杂的“我的页面”,尤其是涉及大量数据加载的情况下,推荐采用虚拟列表或其他性能优化策略[^2]。例如,若需展示历史记录,则可以考虑使用 `recycle-view` 提升滚动体验。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

奶糖 肥晨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值