全局配置
全局配置组件用于提供全局的配置选项,让你的应用下的所有组件都能访问到这些配置。
国际化配置
通过全局配置组件来配置国际化相关的属性,来实现语言切换的功能。
使用两个属性来提供 i18n 相关的配置
暂无数据
- 1
- 2
- 3
- 4
- 5
- 6
- 10
页
共 100 条
vue
<template>
<div>
<el-button mb-2 @click="toggle">Switch Language</el-button>
<br />
<el-config-provider :locale="locale">
<el-table mb-1 :data="[]" />
<el-pagination :total="100" />
</el-config-provider>
</div>
</template>
<script lang="ts" setup>
import { computed, ref } from 'vue'
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import en from 'element-plus/dist/locale/en.mjs'
const language = ref('zh-cn')
const locale = computed(() => (language.value === 'zh-cn' ? zhCn : en))
const toggle = () => {
language.value = language.value === 'zh-cn' ? 'en' : 'zh-cn'
}
</script>
隐藏源代码
按钮配置
vue
<template>
<div>
<div>
<el-checkbox v-model="config.autoInsertSpace">
autoInsertSpace
</el-checkbox>
<el-checkbox v-model="config.plain"> plain </el-checkbox>
<el-checkbox v-model="config.round"> round </el-checkbox>
<el-checkbox v-model="config.text"> text </el-checkbox>
<el-select v-model="config.type" class="ml-5" style="max-width: 150px">
<el-option
v-for="type in buttonTypes.filter(Boolean)"
:key="type"
:value="type"
/>
</el-select>
</div>
<el-divider />
<el-config-provider :button="config">
<el-button>中文</el-button>
</el-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { buttonTypes } from 'element-plus'
const config = reactive({
autoInsertSpace: true,
type: 'default',
plain: true,
round: true,
text: false,
})
</script>
隐藏源代码
链接配置 2.9.11
类型
下划线
vue
<template>
<div>
<div class="flex gap-4">
<div class="flex flex-col basis-150px gap-1">
<span>Type:</span>
<el-select v-model="config.type">
<el-option v-for="type in linkTypes" :key="type" :value="type" />
</el-select>
</div>
<div class="flex flex-col basis-150px gap-1">
<span>Underline:</span>
<el-select v-model="config.underline">
<el-option
v-for="type in underlineOptions"
:key="type"
:value="type"
/>
</el-select>
</div>
</div>
<el-divider />
<el-config-provider :link="config">
<el-link>Link desu!</el-link>
</el-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
const linkTypes = ['primary', 'success', 'warning', 'info', 'danger', 'default']
const underlineOptions = ['always', 'never', 'hover']
const config = reactive({
type: 'success',
underline: 'always',
})
</script>
隐藏源代码
卡片配置 2.10.5
阴影
卡片!
vue
<script lang="ts" setup>
import { reactive } from 'vue'
const config = reactive({
shadow: 'always',
})
</script>
<template>
Shadow:
<div class="flex flex-col justify-center">
<el-radio-group v-model="config.shadow">
<el-radio value="always">always</el-radio>
<el-radio value="hover">hover</el-radio>
<el-radio value="never">never</el-radio>
</el-radio-group>
<el-divider />
<el-config-provider :card="config">
<el-card>Card desu!</el-card>
</el-config-provider>
</div>
</template>
隐藏源代码
对话框配置 2.10.7
垂直居中
可拖拽
可溢出
启用过渡动画
过渡动画: 字符串过渡动画: 对象
vue
<script lang="ts" setup>
import { computed, nextTick, ref, shallowReactive } from 'vue'
import type { ButtonInstance, DialogTransition } from 'element-plus'
type GlobalConfig = {
alignCenter: boolean
draggable: boolean
overflow: boolean
transition?: DialogTransition
}
const config = shallowReactive<GlobalConfig>({
alignCenter: false,
draggable: false,
overflow: false,
})
const visible = ref(false)
const enableTransition = ref(false)
const isObjectTransition = ref(false)
const buttonRef = ref<ButtonInstance>()
const ANIMATION_DURATION = 300
const globalConfig = computed<GlobalConfig>(() => {
let transition: DialogTransition | undefined
if (enableTransition.value) {
if (isObjectTransition.value) {
transition = {
css: false,
onBeforeEnter(el) {
nextTick(() => {
if (buttonRef.value) {
const buttonRect = buttonRef.value.ref!.getBoundingClientRect()
const dialogEl = el.querySelector('.el-dialog') as HTMLElement
if (dialogEl) {
const dialogRect = dialogEl.getBoundingClientRect()
const offsetX =
buttonRect.left +
buttonRect.width / 2 -
(dialogRect.left + dialogRect.width / 2)
const offsetY =
buttonRect.top +
buttonRect.height / 2 -
(dialogRect.top + dialogRect.height / 2)
dialogEl.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(0.3)`
dialogEl.style.opacity = '0'
}
}
})
},
onEnter(el, done) {
nextTick(() => {
const dialogEl = el.querySelector('.el-dialog') as HTMLElement
if (dialogEl) {
// force reflow
dialogEl.offsetHeight
dialogEl.style.transition = `all ${ANIMATION_DURATION}ms cubic-bezier(0.4, 0, 1, 1)`
dialogEl.style.transform = 'translate(0, 0) scale(1)'
dialogEl.style.opacity = '1'
// wait for animation to complete, then cleanup inline styles to avoid affecting drag
setTimeout(() => {
dialogEl.style.transition = ''
dialogEl.style.transform = ''
dialogEl.style.opacity = ''
done()
}, ANIMATION_DURATION)
} else {
done()
}
})
},
onLeave(el, done) {
const dialogEl = el.querySelector('.el-dialog') as HTMLElement
if (dialogEl) {
if (buttonRef.value) {
const buttonRect = buttonRef.value.ref!.getBoundingClientRect()
const dialogRect = dialogEl.getBoundingClientRect()
const currentTransform = dialogEl.style.transform
let dragOffsetX = 0
let dragOffsetY = 0
// avoid draggable effect
if (currentTransform) {
const translateMatch = currentTransform.match(
/translate\(([^,]+),\s*([^)]+)\)/
)
if (translateMatch) {
dragOffsetX = Number.parseFloat(translateMatch[1])
dragOffsetY = Number.parseFloat(translateMatch[2])
}
}
const offsetX =
buttonRect.left +
buttonRect.width / 2 -
(dialogRect.left + dialogRect.width / 2) +
dragOffsetX
const offsetY =
buttonRect.top +
buttonRect.height / 2 -
(dialogRect.top + dialogRect.height / 2) +
dragOffsetY
dialogEl.style.transition = `all ${ANIMATION_DURATION}ms cubic-bezier(0.4, 0, 1, 1)`
dialogEl.style.transform = `translate(${offsetX}px, ${offsetY}px) scale(0.3)`
dialogEl.style.opacity = '0'
// wait for animation to complete, then cleanup inline styles
setTimeout(() => {
dialogEl.style.transition = ''
dialogEl.style.transform = ''
dialogEl.style.opacity = ''
done()
}, ANIMATION_DURATION)
} else {
done()
}
} else {
done()
}
},
}
} else {
transition = 'dialog-bounce'
}
}
return {
alignCenter: config.alignCenter,
draggable: config.draggable,
overflow: config.overflow,
transition,
}
})
</script>
<template>
<div class="flex flex-col gap-4 justify-center">
<div class="flex items-center gap-2">
<el-switch v-model="config.alignCenter" active-text="alignCenter" />
</div>
<div class="flex items-center gap-4">
<el-switch v-model="config.draggable" active-text="draggable" />
<el-switch
v-model="config.overflow"
:disabled="!config.draggable"
active-text="overflow"
/>
</div>
<div class="flex items-center gap-2">
<el-switch v-model="enableTransition" active-text="enable transition" />
<el-switch
v-model="isObjectTransition"
:disabled="!enableTransition"
active-text="transition: object"
inactive-text="transition: string"
/>
</div>
<div class="flex items-center gap-2">
<el-button
ref="buttonRef"
type="primary"
size="small"
@click="visible = true"
>
Open Dialog
</el-button>
</div>
<el-config-provider :dialog="globalConfig">
<el-dialog v-model="visible" title="Dialog Title" destroy-on-close>
Dialog Content
</el-dialog>
</el-config-provider>
<div v-if="enableTransition" class="text-xs opacity-70">
<div v-if="isObjectTransition">
Using JavaScript controlled animation:
<code>{{ JSON.stringify(globalConfig.transition) }}</code>
</div>
<div v-else>
Using string transition name:
<code>{{ globalConfig.transition }}</code>
</div>
</div>
</div>
</template>
<style>
/* Bounce Animation */
.dialog-bounce-enter-active,
.dialog-bounce-leave-active,
.dialog-bounce-enter-active .el-dialog,
.dialog-bounce-leave-active .el-dialog {
transition: all 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}
.dialog-bounce-enter-from,
.dialog-bounce-leave-to {
opacity: 0;
}
.dialog-bounce-enter-from .el-dialog,
.dialog-bounce-leave-to .el-dialog {
transform: scale(0.3) translateY(-50px);
opacity: 0;
}
</style>
隐藏源代码
消息配置
vue
<template>
<div>
<el-config-provider :message="config">
<el-button @click="open">OPEN</el-button>
</el-config-provider>
</div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'
const config = reactive({
max: 3,
plain: true,
placement: 'bottom',
})
const open = () => {
ElMessage('This is a message from bottom.')
}
</script>
隐藏源代码
空值配置 2.7.0
支持的组件列表
- 级联选择器
- 颜色选择器 2.10.3
- 日期选择器
- 选择器
- 虚拟选择器
- 时间选择器
- 时间选择
- 树形选择
设置 empty-values
来支持组件的空值。 回退值是 ['', null, undefined]
。 如果你认为空字符串是有意义的,请写 [undefined, null]
。
设置 value-on-clear
来设置清除时的返回值。 回退值是 undefined
。 在日期组件中是 null
。 如果你想设置为 undefined
,请使用 () => undefined
。
vue
<template>
<el-config-provider :value-on-clear="null" :empty-values="[undefined, null]">
<div class="flex flex-wrap gap-4 items-center">
<el-select
v-model="value1"
clearable
placeholder="Select"
style="width: 240px"
@change="handleChange"
>
<el-option
v-for="item in options"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
<el-select-v2
v-model="value2"
clearable
placeholder="Select"
style="width: 240px"
:options="options"
:value-on-clear="() => undefined"
@change="handleChange"
/>
</div>
</el-config-provider>
</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
const value1 = ref('')
const value2 = ref('')
const options = [
{
value: '',
label: 'All',
},
{
value: 'Option1',
label: 'Option1',
},
{
value: 'Option2',
label: 'Option2',
},
{
value: 'Option3',
label: 'Option3',
},
{
value: 'Option4',
label: 'Option4',
},
{
value: 'Option5',
label: 'Option5',
},
]
const handleChange = (value) => {
if ([undefined, null].includes(value)) {
ElMessage.info(`The clear value is: ${value}`)
}
}
</script>
隐藏源代码
实验性功能
在本节中,你可以学习如何使用全局配置来提供实验性功能。目前,我们还没有添加任何实验性功能,但在未来的路线图中,我们会添加一些实验性功能。你可以使用这个配置来管理你想要或不想要的功能。
API
Config Provider 属性
名称 | 描述 | 类型 | 默认值 |
---|---|---|---|
语言 | 语言对象 | object 语言 | en |
size | 全局组件尺寸 | 枚举 | default |
zIndex | 全局初始 zIndex | number | — |
命名空间 | 全局组件类名前缀 (配合 $namespace) | string | el |
button | 按钮相关配置, 见下表 | object | 见下表 |
链接 | 链接相关配置, 见下表 | object | 见下表 |
对话框 2.10.7 | 对话框相关配置, 见下表 | object | 见下表 |
message | 消息相关配置, 见下表 | object | 见下表 |
实验性功能 | 要添加的处于实验阶段的功能,所有功能默认都设置为 false | object | — |
empty-values 2.7.0 | 组件的全局空值 | array | — |
value-on-clear 2.7.0 | 全局清除返回值 | string / number / boolean / Function | — |
Button 属性
属性 | 描述 | 类型 | 默认值 |
---|---|---|---|
类型 2.9.11 | 按钮类型,当设置 color 时,后者优先 | 枚举 | — |
自动插入空格 | 自动在两个中文字符之间插入一个空格(这只在文本长度为2且所有字符都是中文时生效。) | boolean | false |
朴素按钮 2.9.11 | 判断是否为朴素按钮 | boolean | false |
文字按钮 2.11.0 | 判断是否为文本按钮 | boolean | false |
圆角按钮 2.9.11 | 判断是否为圆形按钮 | boolean | false |
Link 属性
属性 | 描述 | 类型 | 默认值 |
---|---|---|---|
类型 2.9.11 | type | 枚举 | default |
下划线 2.9.11 | 何时应出现下划线 | 枚举 | 悬浮时 |
Card 属性
属性 | 描述 | 类型 | 默认值 |
---|---|---|---|
阴影 2.10.5 | 何时显示卡片阴影 | 枚举 | — |
Dialog 属性
属性 | 描述 | 类型 | 默认值 |
---|---|---|---|
垂直居中 2.10.7 | 对话框是否水平和垂直对齐 | boolean | false |
可拖拽 2.10.7 | 为 Dialog 启用可拖拽功能 | boolean | false |
可溢出 2.10.7 | 可拖拽的对话框可以溢出视口 | boolean | false |
过渡动画 2.10.7 | 对话框动画的自定义过渡配置。可以是一个字符串 (过渡名称) 或一个带有 Vue 过渡 props 的对象 | string / object | — |
Message 属性
属性 | 描述 | 类型 | 默认值 |
---|---|---|---|
max | 可以同时显示的消息最大数量 | number | — |
合并消息 2.8.2 | 合并内容相同的消息,不支持 VNode 类型的消息 | boolean | — |
显示时间 2.8.2 | 显示时长,毫秒。设为 0 则不会自动关闭 | number | — |
显示关闭按钮 2.8.2 | 是否显示关闭按钮 | boolean | — |
偏移量 2.8.2 | 设置距离视口顶部的距离 | number | — |
朴素按钮 2.9.11 | 消息是否为朴素样式 | boolean | — |
placement 2.11.0 | 消息放置位置 | 枚举 | — |
Config Provider 插槽
名称 | 描述 | 作用域 |
---|---|---|
default | 自定义默认内容 | config: 提供的全局配置 (从顶部继承) |