Files
kangyuan-mobile/components/card_pages/card_mescroll.vue
T
2026-05-26 15:52:30 +08:00

145 lines
3.2 KiB
Vue

<template>
<view v-show="props.i === props.index" class="h100">
<mescroll-uni @init="mescrollInit" top="0" :down="downOption" @down="downCallback" :up="upOption"
@up="upCallback" @emptyclick="emptyClick" :height="props.height">
<slot></slot>
</mescroll-uni>
</view>
</template>
<script setup>
import {
ref,
watch,
getCurrentInstance
} from 'vue';
import useMescroll from '@/uni_modules/mescroll-uni/hooks/useMescroll.js';
const {
proxy
} = getCurrentInstance();
const props = defineProps({
i: {
// 每个tab页的专属下标
type: Number,
default () {
return 0;
},
},
index: {
// 当前tab的下标
type: Number,
default () {
return 0;
},
},
tabs: {
// 为了请求数据,演示用,可根据自己的项目判断是否要传
type: Array,
default () {
return [];
},
},
pageSize: {
type: Number,
default: 10,
},
height: {
type: String,
default: '100%',
},
});
watch(
() => props.i,
() => {}, {
immediate: true,
deep: true,
},
);
const {
mescrollInit,
downCallback,
getMescroll
} = useMescroll(); // 调用mescroll的hook
const isAutoInit = props.i === props.index; // 自动加载当前tab的数据
const downOption = {
auto: isAutoInit, // 自动加载当前tab的数据
bottomOffset: 20, // 当手指touchmove位置在距离body底部20upx范围内的时候结束上拉刷新,避免Webview嵌套导致touchend事件不执行
};
const upOption = {
auto: true, // 是否在初始化完毕之后自动执行下拉刷新的回调; 默认true
noMoreSize: 1, // 总数量小于该值也展示“无更多”,设为0确保少于10条时也显示结束
empty: {
tip: '~ 空空如也 ~', // 提示
},
page: {
num: 0, // 当前页码,默认0,回调之前会加1,即callback(page)会从1开始
size: props.pageSize, // 每页数据的数量
},
};
const isInit = ref(isAutoInit); // 当前tab是否已初始化
// 监听下标的变化
watch(
() => props.index,
(val) => {
if (props.i === val && !isInit.value) mescrollTrigger();
},
);
// 主动触发下拉加载
const mescrollTrigger = () => {
isInit.value = true; // 标记为true
const mescroll = getMescroll();
if (mescroll) {
if (mescroll.optDown.use) {
mescroll.triggerDownScroll();
} else {
mescroll.triggerUpScroll();
}
}
};
//点击空布局按钮的回调
const emptyClick = () => {
uni.showToast({
title: '点击了按钮,具体逻辑自行实现',
});
};
const upCallback = (mescroll) => {
setTimeout(() => {
proxy.$emit('upCallback', {
...mescroll,
dataObj: {
status: props.tabs.length ? props.tabs[props.i].value : null,
i: props.i,
index: props.index,
},
});
}, 500);
};
/**
* 根据状态更新 Mescroll 组件的状态
*
* @param state 布尔值,表示 Mescroll 组件的状态,默认为 true
* @param num 数字,表示数据长度,默认为 0
*/
const getState = (state = true, num = 0) => {
const mescroll = getMescroll();
if (state) {
mescroll.endSuccess(num);
} else {
mescroll.endErr();
}
};
defineExpose({
getMescroll,
getState,
mescrollTrigger
}); // 使父组件可以通过ref调用到getMescroll方法 (必须)
</script>
<style lang="scss" scoped></style>