Skip to content

ConfigProvider 全局配置

ConfigProvider 用于为组件树提供全局配置,通过 Vue 的 provide/inject 机制将配置注入到所有子组件中。

支持配置:组件默认尺寸、国际化语言、z-index 起始值、命名空间前缀、以及 Message / Notification / Button 的默认行为。

基础用法

通过 size 属性设置所有子组件的默认尺寸。

尺寸切换

使用 size 控制全部子组件的尺寸,支持 largemediumsmallmini 四种。

国际化语言切换

通过 locale 属性切换全局语言。

暂无数据

Z-Index 配置

设置弹层组件的 z-index 基础值。

此区域内的弹层 z-index 从 5000 开始递增。

命名空间隔离

通过 namespace 属性实现多主题区域的 CSS 类名前缀隔离(需要配合自定义主题样式使用)。

默认命名空间:zc

组件级主题覆写

通过 brandColors 设置品牌色,通过 themeOverrides 按组件粒度覆盖 CSS 变量。

标签

嵌套使用

ConfigProvider 支持嵌套,内层会继承外层未指定的配置,同时覆盖已指定的配置。

外层:large + zh-CN

内层:small(继承 zh-CN)

消息 / 通知默认配置

通过 messagenotification 属性预设消息提示和通知的默认行为。

此区域内的 Message 默认 5 秒后关闭

在代码中读取配置

使用 useGlobalConfig() 组合式函数在任意子组件中读取注入的全局配置。

vue
<script setup lang="ts">
import { useGlobalConfig } from '@zc-ui/components'

const { size, locale, zIndex } = useGlobalConfig()

// size.value → 'small'
// locale.value → 'zh-CN'
// zIndex.value → 5000
</script>

ConfigProvider API

Props

参数说明类型默认值
size全局组件尺寸'large' | 'medium' | 'small' | 'mini'
locale国际化语言代码string
zIndex弹层 z-index 起始值number
namespaceCSS 类名命名空间前缀string'zc'
brandColors品牌色覆盖(自动生成完整色阶)Record<string, string>
themeVariables全局 CSS 变量覆盖ThemeVariables
themeOverrides组件级 CSS 变量覆写ComponentThemeOverrides
button按钮默认配置ButtonConfig
message消息提示默认配置MessageConfig
notification通知默认配置NotificationConfig
as渲染为的 HTML 标签(不设置则为无渲染模式)string

ButtonConfig

参数说明类型默认值
autoInsertSpace是否在两个中文字符之间自动插入空格boolean

MessageConfig

参数说明类型默认值
max同时显示的最大数量number
duration默认关闭时间(毫秒)number
showClose是否显示关闭按钮boolean

NotificationConfig

参数说明类型默认值
max同时显示的最大数量number
duration默认关闭时间(毫秒)number
position默认显示位置'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'

Slots

插槽名说明
default需要应用全局配置的子组件内容

Composable: useGlobalConfig

ts
function useGlobalConfig(): ConfigProviderContext

返回响应式的全局配置上下文,包含 sizelocalezIndexnamespacebuttonmessagenotification 等计算属性。

基于 MIT 许可发布