跳到内容
SCRM DOC

Container 容器组件使用指南

前端组件Container容器创建于2025-03-22更新于 2026-04-22

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 属性

属性类型默认值说明
timeoutnumber-延迟加载时间,指定后无论是否可见都会在指定时间后自动加载
viewportHTMLElement | nullnull视口容器,如果组件在页面容器中滚动,视口就是该容器
thresholdstring'0px'预加载阈值,CSS 单位
direction'vertical'|'horizontal''vertical'滚动方向
tagstring'div'外层容器标签名
maxWaitingTimenumber80组件初始化最大等待时间(毫秒)
transitionNamestring'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 属性

属性类型默认值说明
titlestring''标题
canExpanbooleantrue是否可展开(注意拼写:Expan 非 Expand)
defaultExpanbooleantrue默认是否展开
loadingbooleanfalse是否显示加载状态
helpMessagestring | string[]''标题右侧的帮助提示信息
triggerWindowResizebooleanfalse展开/折叠时是否触发 window.resize,用于表格等自适应高度
lazyTimenumber0延迟加载时间

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

相关组件

页面信息›
已优化是

本页目录