Container 容器组件
页面容器组件,包括滚动容器、懒加载容器、折叠容器等。
ScrollContainer 滚动容器
基本用法
<template>
<ScrollContainer>
<div style="height: 1000px;">
长内容...
</div>
</ScrollContainer>
</template>
<script setup lang="ts">
import { ScrollContainer } from '@/components/Container';
</script>Props 属性
ScrollContainer 继承自 Scrollbar 组件,支持所有原生属性,无额外 Props。
暴露方法
| 方法名 | 参数 | 说明 |
|---|---|---|
| scrollTo | (to: number, duration?: number) => void | 滚动到指定位置(默认持续 500ms) |
| scrollBottom | () => void | 滚动到底部 |
| getScrollWrap | () => Nullable<HTMLElement> | 获取滚动容器 DOM |
设置滚动高度
<template>
<div style="height: 600px;">
<ScrollContainer>
<div v-for="i in 100" :key="i">列表项 {{ i }}</div>
</ScrollContainer>
</div>
</template>
<script setup lang="ts">
import { ScrollContainer } from '@/components/Container';
</script>滚动到指定位置
<template>
<ScrollContainer ref="scrollRef">
<div v-for="i in 50" :key="i" :id="`item-${i}`" style="height: 50px;">
列表项 {{ i }}
</div>
</ScrollContainer>
<a-space>
<a-button @click="scrollToTop">滚动到顶部</a-button>
<a-button @click="scrollToBottom">滚动到底部</a-button>
<a-button @click="scrollToPosition(500)">滚动到 500px</a-button>
</a-space>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import { ScrollContainer } from '@/components/Container';
const scrollRef = ref<InstanceType<typeof ScrollContainer>>();
function scrollToTop() {
scrollRef.value?.scrollTo(0);
}
function scrollToBottom() {
scrollRef.value?.scrollBottom();
}
function scrollToPosition(position: number) {
scrollRef.value?.scrollTo(position, 300); // 300ms 动画
}
</script>LazyContainer 懒加载容器
基于 IntersectionObserver 实现的懒加载容器,当组件进入可视区域时才进行渲染。
基本用法
<template>
<div style="height: 100vh;">
<div style="height: 500px;">上方内容</div>
<LazyContainer @init="handleInit">
<div>进入可视区域后才渲染的内容</div>
</LazyContainer>
</div>
</template>
<script setup lang="ts">
import { LazyContainer } from '@/components/Container';
function handleInit() {
console.log('组件开始初始化');
}
</script>Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| timeout | number | - | 延迟加载时间,指定后无论是否可见都会在指定时间后自动加载 |
| viewport | HTMLElement | null | null | 视口容器,如果组件在页面容器中滚动,视口就是该容器 |
| threshold | string | '0px' | 预加载阈值,CSS 单位 |
| direction | 'vertical'|'horizontal' | 'vertical' | 滚动方向 |
| tag | string | 'div' | 外层容器标签名 |
| maxWaitingTime | number | 80 | 组件初始化最大等待时间(毫秒) |
| transitionName | string | 'lazy-container' | 过渡动画名称 |
Emits 事件
| 事件名 | 参数 | 说明 |
|---|---|---|
| init | () | 组件开始初始化时触发 |
Slots 插槽
| 插槽名 | 说明 |
|---|---|
| default | 默认内容,接收 { loading: boolean } 参数 |
| skeleton | 自定义骨架屏,未指定时使用 Ant Design Vue 的 Skeleton 组件 |
骨架屏示例
<template>
<LazyContainer>
<template #default="{ loading }">
<div v-if="loading">加载中...</div>
<div v-else>实际内容</div>
</template>
<template #skeleton>
<a-skeleton active :paragraph="{ rows: 5 }" />
</template>
</LazyContainer>
</template>
<script setup lang="ts">
import { LazyContainer } from '@/components/Container';
</script>延迟加载示例
<template>
<LazyContainer :timeout="3000" @init="handleInit">
<div>3秒后自动加载</div>
</LazyContainer>
</template>
<script setup lang="ts">
import { LazyContainer } from '@/components/Container';
function handleInit() {
console.log('开始加载');
}
</script>CollapseContainer 折叠容器
基本用法
<template>
<CollapseContainer title="可折叠内容">
<p>这里是可折叠的内容区域</p>
<p>点击标题栏可以展开/折叠</p>
</CollapseContainer>
</template>
<script setup lang="ts">
import { CollapseContainer } from '@/components/Container';
</script>Props 属性
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
| title | string | '' | 标题 |
| canExpan | boolean | true | 是否可展开(注意拼写:Expan 非 Expand) |
| defaultExpan | boolean | true | 默认是否展开 |
| loading | boolean | false | 是否显示加载状态 |
| helpMessage | string | string[] | '' | 标题右侧的帮助提示信息 |
| triggerWindowResize | boolean | false | 展开/折叠时是否触发 window.resize,用于表格等自适应高度 |
| lazyTime | number | 0 | 延迟加载时间 |
Slots 插槽
| 插槽名 | 说明 |
|---|---|
| default | 默认内容 |
| title | 自定义标题 |
| action | 操作区域 |
| footer | 底部内容 |
帮助信息
<template>
<CollapseContainer
title="表单配置"
:help-message="['配置说明1', '配置说明2']"
>
<BasicForm @register="registerForm" />
</CollapseContainer>
<!-- 单条帮助信息 -->
<CollapseContainer
title="用户信息"
help-message="请填写真实的用户信息"
>
<BasicForm @register="registerForm" />
</CollapseContainer>
</template>
<script setup lang="ts">
import { CollapseContainer } from '@/components/Container';
import { useForm } from '@/components/Form';
const [registerForm] = useForm({
schemas: [
{ field: 'name', label: '名称', component: 'Input' },
],
});
</script>默认折叠
<template>
<CollapseContainer
title="高级选项"
:default-expan="false"
>
<p>默认折叠的内容</p>
</CollapseContainer>
</template>
<script setup lang="ts">
import { CollapseContainer } from '@/components/Container';
</script>禁止展开/折叠
<template>
<CollapseContainer
title="固定内容"
:can-expan="false"
>
<p>不能折叠的内容</p>
</CollapseContainer>
</template>
<script setup lang="ts">
import { CollapseContainer } from '@/components/Container';
</script>自定义操作区域
<template>
<CollapseContainer title="用户信息">
<template #action>
<a-space>
<a-button type="primary" size="small">编辑</a-button>
<a-button size="small">删除</a-button>
</a-space>
</template>
<p>内容区域</p>
</CollapseContainer>
</template>
<script setup lang="ts">
import { CollapseContainer } from '@/components/Container';
</script>帮助信息
完整示例:页面布局
<template>
<div class="page-container">
<ScrollContainer scroll-height="calc(100vh - 150px)">
<!-- 顶部固定区域 -->
<CollapseContainer title="基础信息" class="mb-4">
<Description :data="baseInfo" :schema="baseSchema" />
</CollapseContainer>
<!-- 懒加载的详细内容 -->
<LazyContainer :is-loaded="detailLoaded">
<CollapseContainer title="详细信息" class="mb-4">
<Description :data="detailInfo" :schema="detailSchema" />
</CollapseContainer>
<CollapseContainer title="操作记录" class="mb-4">
<BasicTable @register="registerTable" />
</CollapseContainer>
</LazyContainer>
<a-button v-if="!detailLoaded" @click="loadDetail">
加载详情
</a-button>
</ScrollContainer>
</div>
</template>
<script setup lang="ts">
import { ref } from 'vue';
import {
ScrollContainer,
CollapseContainer,
LazyContainer,
} from '@/components/Container';
import { Description, BasicTable, useTable } from '@/components';
const detailLoaded = ref(false);
const baseInfo = ref({
name: '项目名称',
code: 'PRJ001',
status: '进行中',
});
const baseSchema = [
{ field: 'name', label: '项目名称' },
{ field: 'code', label: '项目编号' },
{ field: 'status', label: '状态' },
];
const detailInfo = ref({
desc: '项目描述',
manager: '张三',
startDate: '2024-01-01',
});
const detailSchema = [
{ field: 'desc', label: '描述' },
{ field: 'manager', label: '负责人' },
{ field: 'startDate', label: '开始日期' },
];
const [registerTable] = useTable({
columns: [
{ title: '操作时间', dataIndex: 'time' },
{ title: '操作人', dataIndex: 'operator' },
{ title: '操作内容', dataIndex: 'content' },
],
api: getLogList,
});
async function loadDetail() {
// 模拟加载
await new Promise(resolve => setTimeout(resolve, 500));
detailLoaded.value = true;
}
async function getLogList() {
return [];
}
</script>
<style scoped>
.page-container {
height: 100%;
}
.mb-4 {
margin-bottom: 16px;
}
</style>相关文件
- ScrollContainer:
@/components/Container/src/ScrollContainer.vue - LazyContainer:
@/components/Container/src/LazyContainer.vue - CollapseContainer:
@/components/Container/src/collapse/CollapseContainer.vue - 类型定义:
@/components/Container/src/typing.ts
相关组件
- [[08. BasicTable 表格]] - 表格组件
- 25. Description 描述列表 - 描述列表
页面信息›
已优化是